如何获得关系代数中频率最高的元组?

时间:2018-10-22 18:21:15

标签: sql relational-algebra

id | language
---------
01 | Java
---------
01 | Python
---------
01 | C#
---------
02 | Java

例如,01具有最高的频率,因为他学习最多的语言,那么我如何获得频率最高的id?

2 个答案:

答案 0 :(得分:0)

您可以加入CTE:

with x as (
select id, count(*) as c from my_table group by id
),
m as (select max(c) as t from x)
select x.id
  from x
  join m on x.c = m.t

答案 1 :(得分:0)

select top 1 id, count(id) frequency 
from #mytable
group by id
order by frequency desc