Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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
Sql 如何按分组后在特定列中计数_Sql_Ruby - Fatal编程技术网

Sql 如何按分组后在特定列中计数

Sql 如何按分组后在特定列中计数,sql,ruby,Sql,Ruby,我一直在学习如何编写SQL语句,如果您能教我,我将不胜感激 现状 项目表 身份证件 会话号 项目编号 竞争id 1. 1. 2. 1. 2. 1. 3. 1. 2. 1. 2. 1. 2. 1. 2. 1. 2. 1. 5. 2. 3. 1. 7. 2. 4. 1. 4. 2. 5. 1. 5. 2. 在统计学中,您需要的是模式,即最常见的值 您可以使用聚合和行号(): 如果存在连接,则只返回任意一个值。如果您希望在有领带时使用所有模式,请使用rank()而不是row\u number()

我一直在学习如何编写SQL语句,如果您能教我,我将不胜感激

现状
  • 项目表
身份证件 会话号 项目编号 竞争id 1. 1. 2. 1. 2. 1. 3. 1. 2. 1. 2. 1. 2. 1. 2. 1. 2. 1. 5. 2. 3. 1. 7. 2. 4. 1. 4. 2. 5. 1. 5. 2.
在统计学中,您需要的是模式,即最常见的值

您可以使用聚合和
行号()

如果存在连接,则只返回任意一个值。如果您希望在有领带时使用所有模式,请使用
rank()
而不是
row\u number()

select ct.*
from (select competition_id, item_id, count(*) as cnt,
             row_number() over (partition by competition_id order by count(*) desc) as seqnum
      from t
      group by competition_id, item_id
     ) ci
where seqnum = 1;