根据插入的时间删除重复记录

时间:2016-03-17 06:07:20

标签: sql sql-server

我知道,有很多q&一个为此。但它并没有回复正确的答案。我这里有表格的样本结果。表中有很多数据。但我需要删除具有相同状态的重复记录,col1,col2,col3和createdon只有分钟/秒的差异。

这是我获取重复记录的查询..

WITH CTE AS 
(
    SELECT 
        IssueID AS id, CallID, StatusID AS [status], 
        LevelOneIRCID AS col1, LevelTwoIRCID AS col2, 
        LevelThreeIRCID AS col3, CreatedOn, 
        AddedByUserID AS updatedon, 
        rn = ROW_NUMBER() OVER(PARTITION BY statusid, leveloneircid, leveltwoircid, levelthreeircid, businessunitone, businessunittwo, businessunitthree, Remarks, AddedByUserID --, CreatedOn
                               ORDER BY  leveloneircid, LevelTwoIRCID, LevelThreeIRCID, BusinessUnitOne, BusinessUnitTwo, BusinessUnitThree, Remarks)  
    FROM 
        dbo.Issues 
) 
SELECT * 
FROM CTE 
WHERE rn > 1;

但是当我在分区内添加createdon时,它不算重复,因为在几秒钟内有差异..但我需要考虑这个条件。我该怎么办?请帮助

enter image description here

2 个答案:

答案 0 :(得分:0)

请尝试类似于此('Createdon'的Max和MIN的差异以及按要求列分组)

$category = \App\Category::where('category_name', 'desks')->first();
$products = $category->products;
dd($products);

答案 1 :(得分:0)

WITH CTE AS 
(
    SELECT 
        IssueID AS id, CallID, StatusID AS [status], 
        LevelOneIRCID AS col1, LevelTwoIRCID AS col2, 
        LevelThreeIRCID AS col3, CreatedOn, 
        AddedByUserID AS updatedon, 
        rn = ROW_NUMBER() OVER(PARTITION BY StatusID, LevelOneIRCID, LevelTwoIRCID, LevelThreeIRCID 
                               ORDER BY  CreatedOn)  
    FROM 
        dbo.Issues 
) 
SELECT * 
FROM CTE 
WHERE rn > 1;

CreatedOn中使用Order ByPARTITION BY设置为您未来的唯一键 - StatusID, LevelOneIRCID, LevelTwoIRCID, LevelThreeIRCID。我认为你的查询没问题。