SQL Server重复工资核算记录

时间:2015-01-28 09:54:53

标签: sql sql-server-2008 duplicates duplicate-removal

我一直在尝试从下面的数据集中找到一种方法来选择/删除重复的工资核算记录。

   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

Duplicate是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

非常感谢任何帮助。

谢谢, 亚当

4 个答案:

答案 0 :(得分:0)

如果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)

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我认为你非常接近自己解决这个问题:

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

答案 3 :(得分:0)

CTE As ( SELECT ID,HDate,SiteID,RecordID,EmployeeID,TimeWorked,        Row_Number()Over(通过RecordID按SiteID分区)为rowNum FROM tableName )

- 选择重复记录
来自CTE的SELECT * 其中rowNum> 1