我有两张桌子
Users(user_id, name)
Competition(comp_id, user_id, score)
我需要选择在竞争中发生最多的user_id。 我可以用max(count)来做吗? 如果两个用户的出现次数相同,有没有办法获得异常?
我试过了:
SELECT MAX(numberr), USER_ID
FROM (
SELECT USER_ID, COUNT(COMP_ID) AS numberr
FROM COMPETITION
GROUP BY USER_ID
)
GROUP BY USER_ID;
但我得到的结果是:
MAX(numberr) USER_ID
4 1
2 2
6 3
答案 0 :(得分:1)
您可以使用$('#tablebody').html(data);
$('#rTable').Datatable({
paging:false,
destroy: true
});
答案 1 :(得分:0)
您可以使用not exists
执行此操作。如果有两个用户具有相同的最大计数,则两者都会显示。
with temp as
(
SELECT USER_ID, COUNT(COMP_ID) AS numberr
FROM COMPETITION GROUP BY USER_ID
)
select *
from temp t
where not exists (select 1 from temp where numberr > t.numberr)
答案 2 :(得分:0)
要获得具有最大计数的多个用户,请使用RANK:
SELECT numberr, USER_ID
FROM (
SELECT USER_ID, COUNT(COMP_ID) AS numberr
,RANK() OVER (PARTITION BY USER_ID ORDER BY COUNT(COMP_ID) DESC) rnk
FROM COMPETITION
GROUP BY USER_ID
) dt
WHERE rnk = 1;