sql server 分组查询语句

来源:百度知道 编辑:UC知道 时间:2024/05/06 17:28:05
表:A 111
A 222
A 333
B 444
B 555
B 666
C 777
C 888
C 999

查询结果:A,B,C中前两条记录,请高手指教在sql server 中怎么写语句,急急急急

select top 2 * from table where column='A'
union all
select top 2 * from table where column='B'
union all
select top 2 * from table where column='C'

假设两列的列名分别为Name,Value
select a1.* from 表 as a1 where (select count(a2.*) from 表 as a2 where a2.Name = a1.Name and a2.Value > a1.Value ) < 3