我知道此表中有重复的帐号,但此查询不返回任何结果。
SELECT [CARD NUMBER],[CUSTOMER NAME],[ACCT NBR 1],[ACCT NBR 2],
COUNT([ACCT NBR 1])AS NumOccurences
FROM DebitCardData.dbo。['ATM Checking Accts - Active $']
GROUP BY [卡号],[客户名称],[ACCT NBR 1],[ACCT NBR 2]
HAVING(COUNT([ACCT NBR 1])> 1)
答案 0 :(得分:2)
也许通过“重复的帐号”,您的意思是ACCT NBR 1
和ACCT NBR 2
中的数字(对于相同或不同的记录)?您的查询无法捕捉到这种情况。
答案 1 :(得分:1)
我认为您的查询中没有错误,但这可能对您有用
SELECT [CARD NUMBER],[CUSTOMER NAME],[ACCT NBR 1],[ACCT NBR 2],
NumOccurences
FROM DebitCardData.dbo.['ATM Checking Accts - Active$'] as accMailTbl
inner join
(SELECT [ACCT NBR 1],COUNT([ACCT NBR 1]) AS NumOccurences
FROM DebitCardData.dbo.['ATM Checking Accts - Active$']
GROUP BY [ACCT NBR 1] HAVING (COUNT([ACCT NBR 1])>1)) accTbl
on accTbl.[ACCT NBR 1]=accMailTbl.[ACCT NBR 1]
答案 2 :(得分:0)
据我所知,查询看起来是正确的。向我们展示一些重复数据行,我可以建议查询它们。
答案 3 :(得分:0)
问题是您的数据未规范化。您可以使用以下查询查找重复的帐号,您可以暂存结果或将其嵌套在子查询中以加入回原始数据以获取客户名称和卡号。您的查询的另一个问题是您在组中包含卡号和名称,这意味着如果两个不同的人或卡共享一个帐号,则您将无法检测到它。
编辑:这实际上是我在进行重复搜索时发现的一种非常常见的模式。您必须通过仅在要查找重复项的列上进行分组来检测重复项,然后您必须将其嵌套在子查询中或联接以返回并找出其他数据相关的内容。
Select AccountNumber From
(Select [CARD NUMBER],[CUSTOMER NAME],[ACCT NBR 1] as AccountNumber
From DebitCardData.dbo.['ATM Checking Accts - Active$']
UNION
Select [CARD NUMBER],[CUSTOMER NAME],[ACCT NBR 2] as AccountNumber
From DebitCardData.dbo.['ATM Checking Accts - Active$']
) as NormalizedDebitCardData
GroupBy AccountNumber
Having Count(*)>1
要返回结果中的所有其他列:
Select [CARD NUMBER],[CUSTOMER NAME],[ACCT NBR 1], [ACCT NBR 2]
From DebitCardData.dbo.['ATM Checking Accts - Active$']
Inner Join
(Select AccountNumber From
(Select [CARD NUMBER],[CUSTOMER NAME],[ACCT NBR 1] as AccountNumber
From DebitCardData.dbo.['ATM Checking Accts - Active$']
UNION
Select [CARD NUMBER],[CUSTOMER NAME],[ACCT NBR 2] as AccountNumber
From DebitCardData.dbo.['ATM Checking Accts - Active$']
) as NormalizedDebitCardData
Group By AccountNumber
Having Count(*)>1) as AccountNumberDuplicates
On AccountNumberDuplicates.AccountNumber = [ACCT NBR 1] or AccountNumberDuplicates.AccountNumber = [ACCT NBR 2]
Order By AccountNumberDuplicates.AccountNumber