Sqlserver取分组后的第一条数据
2021-11-10 08:41 NO.27 阅读(469) 评论(0) 编辑 收藏 举报--1 --建立测试环境 CREATE TABLE table1(a VARCHAR(10),b VARCHAR(10),c VARCHAR(10)); --插入数据 INSERT INTO table1 VALUES('2004','12','storea'); INSERT INTO table1 VALUES('2005','07','storea'); INSERT INTO table1 VALUES('2004','11','storeb'); INSERT INTO table1 VALUES('2005','06','storeb'); --2 取分组记录的第一条 SELECT * FROM (SELECT t.*, RANK() OVER (PARTITION BY t.a ORDER BY t.b DESC) AS drank FROM table1 t) a WHERE drank=1