SQL 服务器重复工资单记录

SQL Server Duplicate Payroll Records

我一直在尝试找到一种方法来 select/delete 从我下面的数据集中复制工资单记录。

   ID    HDate      SiteID  RecordID    EmployeeID  TimeWorked
8066839  24/01/2015  164         840          28930       240
7982224  08/11/2014  164         840          28812       390
8066838  23/01/2015  164         839          28930       360
7982223  07/11/2014  164         839          28812       240
8066837  22/01/2015  164         838          28930       330
7982222  06/11/2014  164         838          28812         0

重复的是 RecordID,每个 SiteID 都必须是唯一的。

我无法找到一种方法来为每个 'RecordID' 检索最低的 'ID'。当每个站点需要时,我使用下面的方法查找我的重复项。

    SELECT     *
FROM         StaffHours
WHERE     (SiteID = '164') AND (RecordID IN
                          (SELECT     RecordID
                            FROM          StaffHours
                            WHERE      (SiteID = '164')
                            GROUP BY RecordID
                            HAVING      (COUNT(RecordID) > 1)))
ORDER BY RecordID DESC

我正在尝试类似的操作,但无法正常工作。

    declare dup_id cursor for
SELECT    * FROM         StaffHours
WHERE     (ID IN
                          (SELECT     MIN(ID) AS Expr1
                            FROM          StaffHours
                            WHERE      (SiteID = '25') AND (RecordID IN
                                                       (SELECT     RecordID
                                                         FROM          StaffHours
                                                         WHERE      (SiteID = '25')
                                                         GROUP BY RecordID
                                                         HAVING      (COUNT(RecordID) > 1)))));
                open dup_id;
fetch next from dup_id;
while @@fetch_status = 0
                begin
                                fetch next from dup_id;
                end;
                close dup_id;
                deallocate dup_id
                go

如有任何帮助,我们将不胜感激。

谢谢, 亚当

如果 ID 是一个键或唯一的:

DELETE FROM StaffHours
WHERE ID = 
      (SELECT MIN(ID) FROM StaffHours
        WHERE SiteID = 164
        AND (SELECT count(*) FROM StaffHours sh WHERE sh.RecordID = RecordID AND SiteID = sh.SiteID) > 1
        GROUP BY RecordID)
With CTE As
(
SELECT ID,HDate ,SiteID ,RecordID,EmployeeID ,TimeWorked,
       Row_Number() Over(Partition By SiteID Order By RecordID ) as rowNum
FROM tableName
)

--To select duplicate records   
SELECT * from CTE
Where rowNum>1

--To select unique records
SELECT * from CTE
Where rowNum=1

我认为您已经非常接近自己解决这个问题了:

SELECT     *
FROM         StaffHours
WHERE     (SiteID = '164') AND (ID IN
                      (SELECT     Min(ID)
                        FROM          StaffHours
                        WHERE      (SiteID = '164')
                        GROUP BY RecordID
                        HAVING      (COUNT(RecordID) > 1)))
ORDER BY RecordID DESC

CTE 为 ( SELECT ID,HDate,SiteID,RecordID,EmployeeID,TimeWorked, Row_Number() Over(Partition By SiteID Order By RecordID ) as rowNum FROM 表名 )

--至select条重复记录
SELECT * 来自 CTE 其中 rowNum>1