SQL删除重复行

时间:2016-10-25 08:31:19

标签: sql greatest-n-per-group

我有下一个查询

select No,
       Item,
       Quantity,
       Status
from myTable

执行查询时我得到

enter image description here

我想要区分No,但最后的导入数量是这样的:

enter image description here

仅从

获取此表组中的最后一个数据

3 个答案:

答案 0 :(得分:1)

WITH CTE (No, DuplicateCount)
AS
(
    SELECT No,
    ROW_NUMBER() OVER(PARTITION BY  No
    ORDER BY  No) AS DuplicateCount
    FROM myTable

)
Delete from CTE WHERE DuplicateCount >1

答案 1 :(得分:0)

如果我正确理解了这个问题 试试这个  SELECT * FROM myTable GROUP BY No

答案 2 :(得分:0)

如果您使用的是 MS SQL Server

试试这个

;WITH CTE AS
(
    select ROW_NUMBER() over (partition by [No] order by [No] desc) as rn,*
    from myTable
) 

select No,
       Item,
       Quantity,
       Status
from CTE where rn=1;