我有一个表A,其中有两列名为B和C,如下所示:
('W1','F2')
('W1','F7')
('W2','F1')
('W2','F6')
('W2','F8')
('W4','F7')
('W6','F2')
('W6','F15')
('W7','F1')
('W7','F4')
('W7','F17')
('W8','F13')
如何在oracle中使用sql找出B列中哪一个出现最多? (在这种情况下,它是W2和W7)。谢谢!
答案 0 :(得分:1)
使用子查询计算columnB中每个值的columC中的项目数,并根据该计数计算子查询的结果rank()
。然后在main select中返回columnB的值,其中子查询返回的行的等级为1:
SELECT ColB
FROM (
SELECT ColB,
Count(ColC),
rank() over (ORDER BY Count(ColC) DESC) AS rnk
FROM yourTable
GROUP BY ColB)
WHERE rnk = 1
这是一个sql小提琴:http://sqlfiddle.com/#!4/fa6bd/2
答案 1 :(得分:1)
/ *
* /
WITH T1 AS
(
选择C2,COUNT(*)作为计数
从你的用户到
GROUP BY C2
)
SELECT C2,COUNT FROM T1 WHERE COUNT =(SELECT MAX(COUNT)FROM T1)
;
答案 2 :(得分:0)
Select ColB, Count(*)
FROM yourTable
GROUP BY ColB
ORDER BY count(*) desc