sql server 分組

分組篩選第一條

    SELECT * from(
        SELECT
        *, row_number () OVER (
            partition BY [分組列]
            ORDER BY [排序列] DESC
        ) rowid
        FROM [table]
        WHERE 1=1
    ) t
    WHERE rowid = 1;

分組後刪除多餘的,保留第一條

    delete [table] where 主鍵 in(
        SELECT 主鍵 from(
            SELECT
            *, row_number () OVER (
                partition BY [分組列]
                ORDER BY [排序列] DESC
            ) rowid
            FROM [table]
            WHERE 1=1
        ) t
        WHERE rowid >1
    )

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章