查找列中最常见的条目 - SQL

时间:2012-10-25 13:17:59

标签: mysql sql

我有一个名为MyTable的表,如此

A    B
101  Dog
209  Cat
209  Cat
209  Dog
193  Cow
193  Dog
101  Dog
193  Dog
193  Cow

我希望为每个B提取最常见的A,以便它最终会像这样(请注意可能存在联系)

    A    B
    101  Dog
    209  Cat
    193  Dog
    193  Cow

我怎么能写sql来做到这一点?

4 个答案:

答案 0 :(得分:2)

或者,您可以使用HAVING子句代替JOIN

SELECT A, B
FROM table1 o
GROUP BY A, B
HAVING COUNT(*) = 
    (
        SELECT MAX(totalCOunt)
        FROM
        (
            SELECT A, B, COUNT(*) totalCount
            FROM table1
            GROUP BY A,B
        ) x
        WHERE o.A = x.A 
        GROUP BY x.A
    )

SQLFiddle Demo

答案 1 :(得分:2)

您可以使用过滤联接列出具有最高行数的(A,B)组合:

select  src.*
from    (
        select  A
        ,       B
        ,       count(*) cnt
        from    YourTable
        group by
                A
        ,       B
        ) src
join    (
        select  A
        ,       max(cnt) as maxcnt
        from    (
                select  A
                ,       B
                ,       count(*) cnt
                from    YourTable
                group by
                        A
                ,       B
                ) comb
        group by
                A
        ) maxab
on      maxab.A = src.A
        and maxab.maxcnt = src.cnt

Example at SQL Fiddle.

如果您的数据库支持窗口函数,则可以使用dense_rank(),例如:

select  *
from    (
        select  dense_rank() over (
                    partition by A
                    order by cnt desc) as rn
        ,       *
        from    (
                select  A
                ,       B
                ,       count(*) cnt
                from    YourTable
                group by
                        A
                ,       B
                ) t1
        ) t2
where   rn = 1

Window function example at SQL Fiddle.最新版本的SQL Server,Oracle和PostgeSQL提供了窗口函数。

答案 2 :(得分:1)

select g3.A,g3.B
from
(
    select A,Max(C) MC
    from
    (
        select A,B,count(*) C
        from (<your entire select query>) tbl
        group by A,B
    ) g1
    group by A
) g2
join
(
    select A,B,count(*) C
    from (<your entire select query>) tbl
    group by A,B
) g3 on g2.A=G3.A and g3.C=g2.MaxC

答案 3 :(得分:1)

SQL FIDDLE Example

select
    A, B
from
(
  select
      A, B, row_number() over (partition by A order by cnt desc) as RowNum
  from
  (
    select
       T.A, T.B, count(*) over (partition by T.A, T.B) as cnt
    from T
  ) as A
) as B
where RowNum = 1