如何获取SQLTable中的字符串,分别是top1或top3

How to get strings in SQL Table, which are top1 or top3

这是代码+结果 我尝试了不同的方法,但仍然不知道如何从 CNT 获得 TOP 1 或 TOP 3。

结果应该是这样的 Abon№5 - 800,前 1 名和前 3 名相同

这是代码:

select a.c_name as abonentname, count(*) as cnt
from st_abonents a
inner join qry_type qt on a.id = qt.c_ab_ref
inner join qry_queue qq on qt.id = qq.c_qry_type
group by a.c_name
order by cnt;

结果

我应该添加什么才能让它发挥作用?

在 Oracle 中,您通常使用子查询执行此操作:

select t.*
from (select a.c_name as abonentname, count(*) as cnt
      from st_abonents a inner join
           qry_type qt
           on a.id = qt.c_ab_ref inner join
           qry_queue qq
           on qt.id = qq.c_qry_type
      group by a.c_name
      order by cnt
     ) t
where rownum <= 3;

在最新版本的 Oracle 中,您可以:

      select a.c_name as abonentname, count(*) as cnt
      from st_abonents a inner join
           qry_type qt
           on a.id = qt.c_ab_ref inner join
           qry_queue qq
           on qt.id = qq.c_qry_type
      group by a.c_name
      order by cnt
      fetch first 3 rows only;

你的意思是这样?

select Top(3) a.c_name as abonentname, count(*) as cnt
from st_abonents a
inner join qry_type qt on a.id = qt.c_ab_ref
inner join qry_queue qq on qt.id = qq.c_qry_type
group by a.c_name
order by cnt DESC;