为了设置合并帐户处理方式,我想找出
拥有“完全相同”的所有者。
我认为使用动态sql旋转所有者可能会起作用,然后使用
对功能进行排名,但我不想采用这种方法;我不
对可以与一个名称关联的名称有一个上限
给定帐户,因此我想避免使用动态SQL。
我的数据(也位于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'), --typo in name before mwigdahl's response
(16, 'SVG', 'Wilkie, Walker'), -- corrected typo here too
(606, 'DDA', 'Norman, Storm'),
(606, 'DDA', 'Chase, Billy'),-- corrected 2nd typo found
(4, 'LNS', 'Wilkie, Walker'),
(4, 'LNS', 'Houzemeal, Juvy'),
(44, 'DDA', 'Perkins, Tony'),
(222, 'DDA', 'Wilkie, Walker'),
(222, 'DDA', 'Houzemeal, Juvy'),
(17, 'SVG', 'Wilkie, Walker'), -- added these three rows in edit, SVG 17 doesn't match any dda
(17, 'SVG', 'Welch, Raquel'),
(17, 'SVG', 'Houzemeal, Juvy')
我想找出每个MODULE-ACCOUNT最低的DDA
帐户拥有完全相同的所有者
与之相关联。
在示例数据中,我需要这些结果,第三列是
具有相同所有者的最低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和SVG 16与任何DDA帐户均不匹配,因此这无关紧要
它们彼此匹配,它们将为合并帐户获得NULL。编辑:SVG 17与任何内容都不匹配,即使在SVG 17中有一个DDA acct拥有所有持有人的情况下,对于任何一个DDA acct都不会发生SVG 17中持有人的组合。每个DDA帐户都会匹配自己,除非
拥有相同所有者且DDA较低的dda帐户(DDA 222就是这种情况)。
我可以看到一种通用的方法是将每个帐户,组
数据透视表,并使用row_number。鉴于无数
与每个帐户相关联的持有人,我认为转折将需要
我宁愿避免使用动态SQL。
在我看来,这是一个“关系分歧”问题,
关系部门可能由“交叉申请”“喂食”。我试过了
编写一个函数,该函数将采用与之相关的帐户持有人表格
与特定的帐户,并找到最低的dda帐户
如下所示,其想法是查看给定人数是否全部
该帐户与加入该帐户时的人数相同
到给定的dda帐户,但我不知道如何“喂”表格
功能中的帐号。
-- this is what I tried but I'm not sure it the logic would work
-- and I can't figure out how to pass the account holders for each
-- account in. This is a bit changed from the function I wrote, some
-- extraneous fields removed and cryptic column names changed. So it
-- probably won't run as is.
-- to support a parameter type to a tape
-- CREATE type VisionCustomer as Table
-- (customer varchar(30))
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;
最佳答案
我相信这是您要寻找的(http://www.sqlfiddle.com/#!3/f96c5/1):
;WITH AccountsWithOwners AS
(
SELECT DISTINCT
DA.module
, DA.account
, STUFF((SELECT
',' + AAR.custCode
FROM allacctRels AAR
WHERE AAR.module = DA.module
AND AAR.account = DA.account
ORDER BY AAR.custCode
FOR XML PATH(''))
, 1, 1, '') AS Result
FROM allacctRels DA
)
, WithLowestDda AS
(
SELECT
AWO.module
, AWO.account
, MatchingAccounts.account AS DdaAccount
, ROW_NUMBER() OVER(PARTITION BY AWO.module, AWO.account ORDER BY MatchingAccounts.account) AS Row
FROM AccountsWithOwners AWO
LEFT JOIN AccountsWithOwners MatchingAccounts
ON MatchingAccounts.module = 'DDA'
AND MatchingAccounts.Result = AWO.Result
)
SELECT
account
, module
, DdaAccount
FROM WithLowestDda
WHERE Row = 1
关于sql-server - 如何找到与其他记录组匹配的记录组(关系划分?),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10094886/