时间检索的SQL语句如何写
发布网友
发布时间:2022-04-07 17:47
我来回答
共3个回答
热心网友
时间:2022-04-07 19:16
lopezzhy的回答,限定了a只有A、B两个值,如果是A、B、C、D、E、F、G......呢?
用子查询:
select * from AAA as t
where (select count(*) from AAA
where a= t.a and c>= t.c) <=5
order by a, c desc
热心网友
时间:2022-04-07 20:34
select a,sum(b) as tot_b
from (select top 4 a,b from AAA where a='A' order by c desc) x
group by a
union all
select a,sum(b) as tot_b
from (select top 4 a,b from AAA where a='B' order by c desc) y
group by a
热心网友
时间:2022-04-07 22:09
sql 2005以上版本及Oracle使用这个:
select a,count(*) ct from (select a,b,row_number() over (partition by a order by b desc) rn from AAA) BBB where rn<=5 group by a;
sql 2000参见 lopezzhy的回答。不过把top 4 改为top 5