To set up consolidated account processing, I want to find out accounts that have the “exact same” set of owners.
I think this might work to deploy dynamic sql owners and then use it but I don't want to continue this approach; I do not have an upper limit on the number of names that can be associated with this account, so I want to avoid dynamic SQL.
My details (also this is at http://www.sqlfiddle.com/#!3/1d36e )
CREATE TABLE allacctRels
(account INT NOT NULL,
module CHAR(3) NOT NULL,
custCode CHAR(20) NOT NULL)
INSERT INTO allacctrels
(account, module, custCode)
VALUES
(1, 'DDA', 'Wilkie, Walker'),
(1, 'DDA', 'Houzemeal, Juvy'),
(2, 'CDS', 'Chase, Billy'),
(2, 'CDS', 'Norman, Storm'),
(3, 'CDS', 'Chase, Billy'),
(3, 'CDS', 'Norman, Storm'),
(7, 'CDS', 'Perkins, Tony'),
(15, 'SVG', 'Wilkie, Walker'),
(16, 'SVG', 'Wilkie, Walker'),
(606, 'DDA', 'Norman, Storm'),
(606, 'DDA', 'Chase, Billy'),
(4, 'LNS', 'Wilkie, Walker'),
(4, 'LNS', 'Houzemeal, Juvy'),
(44, 'DDA', 'Perkins, Tony'),
(222, 'DDA', 'Wilkie, Walker'),
(222, 'DDA', 'Houzemeal, Juvy'),
(17, 'SVG', 'Wilkie, Walker'),
(17, 'SVG', 'Welch, Raquel'),
(17, 'SVG', 'Houzemeal, Juvy')
I want to find out, for each MODULE-ACCOUNT, what is the lowest DDA account that the same owners have associated with it.
,
DDA, . , -/ - SELECT DISTINCT, FROM allAcctRels)
1, DDA, 1
2, CDS, 606
3, CDS, 606
15, SVG, NULL
16, SVG, NULL
606, DDA, 606
4, LNS, 1
7, CDS, 44
44, DDA, 44
222, DDA, 1
17, SVG, NULL -- added to original post.
SVG 15 16 DDA, ,
, NULL . EDIT: SVG 17 , SVG 17 DDA acct, , SVG 17 DDA acct. DDA ,
dda DDA ( DDA 222).
, , ,
row_number.
, , ,
SQL, .
, " ",
, , "" .
,
dda,
, , ,
, ,
dda, , ""
.
CREATE FUNCTION consolidatable
(@custList dbo.VisionCustomer READONLY)
RETURNS char(10)
AS
BEGIN
DECLARE @retval Varchar(10)
DECLARE @howmany int
select @howmany=Count(*) FROM @custlist;
SELECT @retval = min (acct) FROM allAcctRels
JOIN @custlist
On VendorCustNo = Customer
WHERE acctType = 'DDA'
GROUP BY acct
HAVING (count(*) = @howmany)
and
COUNT(*) = (select Count(*) FROM allAcctRels X
WHERE X.acctType = 'DDA'
AND X.account = AllAcctRels.account) ;
RETURN @retval
END;