在SQL中的“group by”表达式:如何获取组中最常出现的字符串?

时间:2009-09-01 17:56:00

标签: sql oracle oracle10g group-by

假设我们有下表:

Id   A    B
1    10   ABC
2    10   ABC
3    10   FFF
4    20   HHH

作为“group by A”表达式的结果,我希望得到最常出现的B-Column的值:

select A, mostoften(B) from table group by A;

A    mostoften(B)
10   ABC
20   HHH

如何在Oracle 10g中实现这一目标?

备注:如果出现平局(当最多出现一个以上的值时),选择哪个值无关紧要。

4 个答案:

答案 0 :(得分:5)

select A, B
from (
  select A, B, ROW_NUMBER() OVER (PARTITION BY A ORDER BY C_B DESC) as rn
  from (
     select A, COUNT (B) as C_B, B
     from table
     group by A, B
  ) count_table
) order_table
where rn = 1;

你希望Bs的最大COUNT组由A,B。

组成

答案 1 :(得分:1)

老派的解决方案,我花了一些时间和一些诅咒:)

select a,b
from   ta ta1
group by a,b
having count(*) = (select max(count(*))
                   from   ta ta2
                   where  ta1.a = ta2.a
                   group by b)

答案 2 :(得分:0)

试试这个(适用于SQL Server 2005):

declare @yourtable table (rowid int, a int,b char(3))
insert into @yourtable values (1,10,'ABC')
insert into @yourtable values (2,10,'ABC')
insert into @yourtable values (3,10,'FFF')
insert into @yourtable values (4,20,'HHH')

;WITH YourTableCTE AS
(
SELECT
    *, ROW_NUMBER() OVER(partition by A ORDER BY A ASC,CountOfB DESC) AS RowRank
    FROM (SELECT 
              A, B, COUNT(B) AS CountOfB
              FROM @yourtable
              GROUP BY A,B
          ) dt
)
SELECT
    A,B
    FROM YourTableCTE
    WHERE RowRank=1
没有CTE的

编辑 ......

SELECT
    A,B
    FROM (SELECT
              *, ROW_NUMBER() OVER(partition by A ORDER BY A ASC,CountOfB DESC) AS RowRank
              FROM (SELECT 
                        A, B, COUNT(B) AS CountOfB
                        FROM @yourtable
                        GROUP BY A,B
                    ) dt
         ) dt2



    WHERE RowRank=1

答案 3 :(得分:0)

通过在每个A& A中创建一个计数视图,可以澄清这个问题。 B组:

CREATE VIEW MyTableCounts AS
  SELECT A, B, COUNT(*) C
  FROM MyTable
  GROUP BY A, B;

现在我们可以进行查询,找到计数最大的行c1。也就是说,没有其他具有相同A的行具有更大的计数。因此,如果我们尝试查找具有更多计数的行c2,则找不到匹配项。

SELECT c1.A, c1.B
FROM MyTableCounts c1
LEFT OUTER JOIN MyTableCounts c2
  ON (c1.A = c2.A AND (c1.C < c2.C OR (c1.C = c2.C AND c1.B < c2.B)))
WHERE c2.A IS NULL
ORDER BY c1.A;

要解决绑定计数(c1.C = c2.C),我们使用B的值,我们知道A的给定组中的值是唯一的。