数据库
首页 > 数据库> > sql server 分组

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
    )

 

标签:server,分组,rowid,sql,table,WHERE,主键,SELECT
来源: https://www.cnblogs.com/yyutudou/p/15797853.html