我有一个TRANSACTION表。 列是=
Personnel_id和Personnel_Name
我想找到最重复的数据排序。 我怎么能这样做? 我试过,我发现,最重复的结果,但我不能显示personnel_name。
这是我的查询;
SELECT PERSONNEL_ID,
COUNT(PERSONNEL_ID)
FROM KOMTAS.TRANSACTIONS
GROUP BY PERSONNEL_ID;
此代码出错;
SELECT MAX(R),
PERSONNEL_ID
FROM ( SELECT PERSONNEL_ID,
COUNT(PERSONNEL_ID) R
FROM KOMTAS.TRANSACTIONS
GROUP BY PERSONNEL_ID
) ;
请帮忙!
答案 0 :(得分:0)
在第二个查询中添加第二个组:
SELECT MAX(R),
PERSONNEL_ID
FROM ( SELECT PERSONNEL_ID,
COUNT(PERSONNEL_ID) R
FROM KOMTAS.TRANSACTIONS
GROUP BY PERSONNEL_ID )
GROUP BY PERSONNEL_ID;
答案 1 :(得分:0)
我建议使用rownum
和子查询:
SELECT t.*
FROM (SELECT PERSONNEL_ID, COUNT(PERSONNEL_ID) as cnt
FROM KOMTAS.TRANSACTIONS
GROUP BY PERSONNEL_ID
ORDER BY COUNT(PERSONNEL_ID) DESC
) t
WHERE rownum = 1;
答案 2 :(得分:0)
试试这个
SELECT MAX(cnt) AS c,
PersonId
FROM ( SELECT PersonId,
COUNT(PersonId) cnt
FROM TRANSACTIONS
GROUP BY PersonId )
GROUP BY PersonId
ORDER BY c DESC;
点击此处SQLFiddle