Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 查找列中最常见的条目-SQL_Mysql_Sql - Fatal编程技术网

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

Mysql 查找列中最常见的条目-SQL,mysql,sql,Mysql,Sql,我有一张桌子叫MyTable,就像这样 A B 101 Dog 209 Cat 209 Cat 209 Dog 193 Cow 193 Dog 101 Dog 193 Dog 193 Cow 我想为每一个A找出最常见的B,这样它就会像这个音符一样,有联系 A B 101 Dog 209 Cat 193 Dog 193 Cow 如何编写sql来实现这一点?或者,您可以使用HAVING子句而不是JOIN 您可以使用筛选

我有一张桌子叫MyTable,就像这样

A    B
101  Dog
209  Cat
209  Cat
209  Dog
193  Cow
193  Dog
101  Dog
193  Dog
193  Cow
我想为每一个A找出最常见的B,这样它就会像这个音符一样,有联系

    A    B
    101  Dog
    209  Cat
    193  Dog
    193  Cow

如何编写sql来实现这一点?

或者,您可以使用HAVING子句而不是JOIN


您可以使用筛选联接列出行数最高的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
如果您的数据库支持窗口功能,则可以使用稠密排列,如:

在最新版本的SQL Server、Oracle和PostgeSQL上提供了窗口功能

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

我不知道这是什么意思。SQL server?MySQL?神谕DB2?或者任何..仅供参考,RDBMS代表关系数据库管理System@CodeGuy谢谢,你能看到我的答案吗?你能修改它来处理这样一个事实,即tbl实际上是一个在其内部返回这两个参数的查询吗columns@CodeGuy-请参见编辑。您只需将整个查询放在括号中,括号将化名为tbland+1,以便我了解SQLFIDLE:
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
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
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