永发信息网

一条SQL语句好难,帮我看看怎么实现?

答案:3  悬赏:60  手机版
解决时间 2021-04-22 13:16
  • 提问者网友:几叶到寒
  • 2021-04-21 22:47

表tab1,字段a,t

运行:

select a,count(*) as b from tab1 group by a

结果:

a b

ww 510

qq 222

tt 111

---------

我要实现这样的功能:取出a列为ww,qq,tt....的10个t字段的内容,怎么实现?即每种10个。

最佳答案
  • 五星知识达人网友:山河有幸埋战骨
  • 2021-04-21 23:09

我来给你答案



select id=identity(int,1,1),a,t into ##tb from tb1
select * from ##tb,(select a,Min(id) as tag from ##tb group by a) tb where ##tb.id<tag+10 and ##tb.id>=tag

全部回答
  • 1楼网友:街头电车
  • 2021-04-22 00:55

oracle:

select tab2.t,tab2.a from (select distinct a,t,dense_rank() over(partition by a order by t) tt from tab1) tab2 where tab2.tt<=10;

  • 2楼网友:英雄的欲望
  • 2021-04-21 23:43

select top10 a,count(*) as b from tab1 where a='ww' group by a union all

select top10 a,count(*) as b from tab1 where a='qq' group by a union all

select top10 a,count(*) as b from tab1 where a='tt' group by a union all

...

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯