如何从中数最大的表中获得eid?

时间:2019-03-28 07:28:32

标签: sql sql-server

我有一个员工表,其结构如下:

Id.      Mid    Salary

1          20        200
2          20       3000
3         30       200
4         34       4000
5        30         300
6        30        400
1        23        440
1        24         333
2         21        3

我想得到类似这样的结果

Id     Mid
1      3

2 个答案:

答案 0 :(得分:1)

使用前1个

select top 1 id,count(*) as cnt
from table   
group by id
order by cnt desc

如果您需要领带,请使用with ties

select top 1 with ties id,count(*) as cnt
    from table   
    group by id
    order by cnt desc

答案 1 :(得分:1)

您可以尝试以下操作-使用top with ties和聚合

select top 1 with ties  id, count(mid) as Mid
from table
group by id
order by count(mid) desc