Sqlserver取分组后的第一条数据

--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

 

上一篇:MySQL_Ch5


下一篇:Spring Cloud Alibaba学习08--Seata基本使用