我有这个:
SELECT ActionDateTime, [Action Location], FirstName, MiddleName, LastName,
IDNumber, IDNumber2, IDNumber3, Department, CardData_BadgePass, Location, Birthdate
FROM dbo.EmployeeEntityTracking AS t
WHERE EXISTS (SELECT CAST(ActionDateTime AS DATE) AS Expr1, [Action Location], FirstName,
MiddleName, LastName, IDNumber, IDNumber2, IDNumber3, Department, CardData_BadgePass, Location, Birthdate
FROM dbo.EmployeeEntityTracking AS x
WHERE (IDNumber = t.IDNumber) AND (ActionDateTime <> t.ActionDateTime))
Wich返回带有重复项的记录,但我需要按日期过滤。 (查询返回重复但是所有日期。我需要在每个日期重复
答案 0 :(得分:1)
尝试使用group by:
SELECT CAST(ActionDateTime AS DATE)
,[Action Location]
,FirstName
,MiddleName
,LastName
,IDNumber
,IDNumber2
,IDNumber3
,Department
,CardData_BadgePass
,Location
,Birthdate
FROM dbo.EmployeeEntityTracking AS t
GROUP BY
CAST(ActionDateTime AS DATE)
,[Action Location]
,FirstName
,MiddleName
,LastName
,IDNumber
,IDNumber2
,IDNumber3
,Department
,CardData_BadgePass
,Location
,Birthdate
HAVING COUNT(IDNumber) > 1
PS:它只会显示所有列的重复项,如果只需要ID和日期,请尝试排除所有项目。
答案 1 :(得分:0)
首先,没有必要列出exists子句中的所有列,只需用任何固定值替换它们,因为您正在寻找存在不寻找返回列值的行
因为问题不是那么清楚,我根据您可能要求的案例提供了许多建议:
1)如果您希望根据IDNUMBER
和ActionDateTime
SELECT * FROM(
SELECT DISTINCT ActionDateTime, [Action Location], FirstName, MiddleName, LastName,
IDNumber, IDNumber2, IDNumber3, Department, CardData_BadgePass, Location, Birthdate
,COUNT(ActionDateTime) OVER(PARTITION BY ActionDateTime,IDNUMBER) AS [ItemCount]
FROM dbo.EmployeeEntityTracking AS t
WHERE EXISTS (SELECT 1
FROM dbo.EmployeeEntityTracking AS x
WHERE (x.IDNumber = t.IDNumber) AND (x.ActionDateTime <> t.ActionDateTime))) AS T1
WHERE T1.ItemCount > 1
ORDER BY T1.ActionDateTime
2)如果我理解您要找的内容,您可以使用DISTINCT
功能根据所有列消除重复
SELECT DISTINCT ActionDateTime, [Action Location], FirstName, MiddleName, LastName,
IDNumber, IDNumber2, IDNumber3, Department, CardData_BadgePass, Location, Birthdate
FROM dbo.EmployeeEntityTracking AS t
WHERE EXISTS (SELECT 1
FROM dbo.EmployeeEntityTracking AS x
WHERE (x.IDNumber = t.IDNumber) AND (x.ActionDateTime <> t.ActionDateTime))
3)如果您要查找具有相同重复日期的行
SELECT * FROM (
SELECT DISTINCT ActionDateTime, [Action Location], FirstName, MiddleName, LastName,
IDNumber, IDNumber2, IDNumber3, Department, CardData_BadgePass, Location, Birthdate
,COUNT(ActionDateTime) OVER(PARTITION BY ActionDateTime) AS [DateCount]
FROM dbo.EmployeeEntityTracking AS t
WHERE EXISTS (SELECT 1
FROM dbo.EmployeeEntityTracking AS x
WHERE (x.IDNumber = t.IDNumber) AND (x.ActionDateTime <> t.ActionDateTime))) AS T1
WHERE T1.DateCount > 1
ORDER BY T1.ActionDateTime
4)如果您想要消除包含相同日期的所有行:
SELECT * FROM (
SELECT DISTINCT ActionDateTime, [Action Location], FirstName, MiddleName, LastName,
IDNumber, IDNumber2, IDNumber3, Department, CardData_BadgePass, Location, Birthdate
,COUNT(ActionDateTime) OVER(PARTITION BY ActionDateTime) AS [DateCount]
FROM dbo.EmployeeEntityTracking AS t
WHERE EXISTS (SELECT 1
FROM dbo.EmployeeEntityTracking AS x
WHERE (x.IDNumber = t.IDNumber) AND (x.ActionDateTime <> t.ActionDateTime))) AS T1
WHERE T1.DateCount < 2
ORDER BY T1.ActionDateTime