我需要查找FIRSTNAME是重复且LASTNAME是重复的所有记录
所以我的数据有点像这样:
FirstName LastName CustomerFileLocation
Joe Smith c:\file1
Joe Jones c:\File2
Joe Smith c:\File3
Harry Smith c:\File4
我希望查询返回
Joe Smith c:\file1
Joe Smith c:\File3
我写了这个来选择它们,但如果我将CustomerFileLocation添加到GROUP BY表达式,它就不会返回任何内容。
SELECT FirstName, LastName, COUNT(1) AS CNT, CustomerFileLocation
FROM tblCustomerList
WHERE (Skip = 0)
GROUP BY FirstName, LastName
HAVING (COUNT(1) > 1)
答案 0 :(得分:0)
SELECT (FirstName + ' ' + LastName) as fullname,
cast(CustomerFileLocation as nvarchar) as location
FROM tblCustomerList WHERE (Skip = 0) GROUP BY (FirstName + ' ' + LastName),
cast(customerFileLication as nvarchar) having count(1)>1
我还没有对其性能进行测试或调整,但这条线必须有效。
答案 1 :(得分:0)
您可以尝试使用子查询返回包含任何计数大于1的记录的表,然后将该表连接回该子查询。
没有尝试过,但它看起来像这样。
SELECT tblCustomerList.FirstName, tblCustomerList.LastName, tblCustomerList.CustomerFileLocation From (SELECT FirstName, LastName, COUNT(1) AS CNT, CustomerFileLocation
FROM tblCustomerList
WHERE
GROUP BY FirstName, LastName
HAVING (COUNT(1) > 1))) as countTable inner join tblCustomerList
on countTable.lastname = tblCustomerList.lastname and countTable.firstname = tblCustomerList.firstname and tblCustomerList.Skip = 0