Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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_Sqlite_Group By_Count - Fatal编程技术网

Sql 如何确定组中计数最高的类别?

Sql 如何确定组中计数最高的类别?,sql,sqlite,group-by,count,Sql,Sqlite,Group By,Count,我正在按子字符串组计算表中所有的商店缩写。我有另一个名为city的专栏,显示了商店缩写的位置。现在,我还想选择/显示子字符串组最常出现的城市,如果可能,还想显示该城市的shopabb计数。结果应该是这样的: select substr(shopabb, 1, 4) as shopgroup, count(*) from table where shopabb like'h%' group by substr(shoppabb, 1, 4) order by count(*) DESC; 谢谢

我正在按子字符串组计算表中所有的商店缩写。我有另一个名为city的专栏,显示了商店缩写的位置。现在,我还想选择/显示子字符串组最常出现的城市,如果可能,还想显示该城市的shopabb计数。结果应该是这样的:

select substr(shopabb, 1, 4) as shopgroup, count(*)
from table
where shopabb like'h%'
group by substr(shoppabb, 1, 4)
order by count(*) DESC;
谢谢

使用具有两个聚合级别的
行编号()

    shopabb   Count(*)   City      Count_City
    -----------------------------------------
   hel         50         London    40
   heal        20         Berlin    15   
   hot         10         Rome       8
是一个dbfiddle,说明语法是有效的

编辑:

您可以在不使用窗口功能的情况下执行此操作,但它要麻烦得多:

select shopgroup, sum(cnt) as total_count, 
       max(case when seqnum = 1 then city end) as city_with_max,
       max(case when seqnum = 1 then cnt end) as cnt_at_max
from (select substr(shopabb, 1, 4) as shopgroup, city, count(*) as cnt,
             row_number() over (partition by substr(shopabb, 1, 4) order by count(*) desc) as seqnum
      from table
      where shopabb like'h%'
      group by substr(shopabb, 1, 4), city
     ) t
group by shopgroup
order by sum(cnt) DESC;
请注意,如果两个城市的最大值相等,则这将返回重复值。要获得总的总数也需要更多的工作。

使用
行数()
和两个聚合级别:

    shopabb   Count(*)   City      Count_City
    -----------------------------------------
   hel         50         London    40
   heal        20         Berlin    15   
   hot         10         Rome       8
是一个dbfiddle,说明语法是有效的

编辑:

您可以在不使用窗口功能的情况下执行此操作,但它要麻烦得多:

select shopgroup, sum(cnt) as total_count, 
       max(case when seqnum = 1 then city end) as city_with_max,
       max(case when seqnum = 1 then cnt end) as cnt_at_max
from (select substr(shopabb, 1, 4) as shopgroup, city, count(*) as cnt,
             row_number() over (partition by substr(shopabb, 1, 4) order by count(*) desc) as seqnum
      from table
      where shopabb like'h%'
      group by substr(shopabb, 1, 4), city
     ) t
group by shopgroup
order by sum(cnt) DESC;

请注意,如果两个城市的最大值相等,则这将返回重复的结果。要获得总的总数也需要更多的工作。

[09:37:54]在数据库“表”上执行SQL查询时出错:near“(”:语法错误。我认为分区依据前的括号是问题所在。@remotesatellite…语法错误是由于您使用
table
引用表而引起的。请输入表名。我只是使用了您在问题中提供的内容。我替换了所有列名和表名。但我仍然得到相同的语法错误。SQLite从3.25.0版开始就支持窗口函数。当前版本为3.32。可能您使用的是旧版本。我使用的是3.22.0。这应该可以解释问题。[09:37:54]在数据库“表”上执行SQL查询时出错:near“(”:语法错误。我认为分区依据前的括号是问题所在。@remotesatellite…语法错误是由于您使用
table
引用表而引起的。请输入表名。我只是使用了您在问题中提供的内容。我替换了所有列名和表名。但我仍然得到相同的语法错误。SQLite从3.25.0版开始就支持窗口功能。当前版本是3.32。可能您使用的是旧版本。我使用的是3.22.0。这应该可以解释问题。