SQL查询中分组COUNT的SUM

时间:2012-10-17 04:45:10

标签: sql count sum

我有一个包含2个字段的表:

ID  Name
--  -------
1   Alpha
2   Beta
3   Beta
4   Beta
5   Charlie
6   Charlie

我想按名称对它们进行分组,使用'count'和一行'SUM'

Name     Count
-------  -----
Alpha     1
Beta      3
Charlie   2
SUM       6

如何编写查询以在表格下方添加SUM行?

16 个答案:

答案 0 :(得分:57)

SELECT name, COUNT(name) AS count
FROM table
GROUP BY name

UNION ALL

SELECT 'SUM' name, COUNT(name)
FROM table

<强>输出:

name                                               count
-------------------------------------------------- -----------
alpha                                              1
beta                                               3
Charlie                                            2
SUM                                                6

答案 1 :(得分:44)

SELECT name, COUNT(name) AS count, SUM(COUNT(name)) OVER() AS total_count
FROM Table GROUP BY name

答案 2 :(得分:10)

不指定您正在使用的rdbms

看一下这个演示

SQL Fiddle DEMO

SELECT Name, COUNT(1) as Cnt
FROM Table1
GROUP BY Name
UNION ALL
SELECT 'SUM' Name, COUNT(1)
FROM Table1

那就是说,我会建议你的表示层添加总数,而不是数据库。

这是使用Summarizing Data Using ROLLUP

的SQL SERVER版本

SQL Fiddle DEMO

SELECT CASE WHEN (GROUPING(NAME) = 1) THEN 'SUM'
            ELSE ISNULL(NAME, 'UNKNOWN')
       END Name, 
      COUNT(1) as Cnt
FROM Table1
GROUP BY NAME
WITH ROLLUP

答案 3 :(得分:6)

试试这个:

SELECT  ISNULL(Name,'SUM'), count(*) as Count
FROM table_name
Group By Name
WITH ROLLUP

答案 4 :(得分:4)

请按以下方式运行:

Select sum(count) 
  from (select Name, 
               count(Name) as Count 
          from YourTable
      group by Name);  -- 6

答案 5 :(得分:2)

您可以使用union来加入行。

select Name, count(*) as Count from yourTable group by Name
union all
select "SUM" as Name, count(*) as Count from yourTable

答案 6 :(得分:2)

这里的所有解决方案都很棒,但不一定可以在旧的mysql服务器上实现(至少就我而言)。因此您可以使用子查询(我认为它不那么复杂)。

 select sum(t1.cnt) from 
        (SELECT column, COUNT(column) as cnt
            FROM
            table 
            GROUP BY 
            column
            HAVING 
            COUNT(column) > 1) as t1 ;

答案 7 :(得分:2)

我解释这个问题的方式是需要每组答案的小计值。使用PARTITION

小计非常容易
SUM(COUNT(0)) OVER (PARTITION BY [Grouping]) AS [MY_TOTAL]

这是我的完整SQL调用的样子:

SELECT MAX(GroupName) [name], MAX(AUX2)[type],  
COUNT(0) [count], SUM(COUNT(0)) OVER(PARTITION BY GroupId) AS [total]
    FROM [MyView]
  WHERE Active=1 AND Type='APP' AND Completed=1
    AND [Date] BETWEEN '01/01/2014' AND GETDATE()
    AND Id = '5b9xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' AND GroupId IS NOT NULL
  GROUP BY AUX2, GroupId

从中返回的数据如下:

name    type    count   total
Training Group 2    Cancelation 1   52
Training Group 2    Completed   41  52
Training Group 2    No Show 6   52
Training Group 2    Rescheduled 4   52
Training Group 3    NULL        4   10535
Training Group 3    Cancelation 857 10535
Training Group 3    Completed   7923    10535
Training Group 3    No Show 292 10535
Training Group 3    Rescheduled 1459    10535
Training Group 4    Cancelation 2   27
Training Group 4    Completed   24  27
Training Group 4    Rescheduled 1   27

答案 8 :(得分:1)

  with cttmp
  as
  (
  select Col_Name, count(*) as ctn from tab_name group by Col_Name having count(Col_Name)>1
  )
  select sum(ctn) from c

答案 9 :(得分:1)

您可以使用ROLLUP

select nvl(name, 'SUM'), count(*)
from table
group by rollup(name)

答案 10 :(得分:0)

您可以尝试按名称分组并计算该组中的ID。

SELECT name, count(id) as COUNT FROM table group by name

答案 11 :(得分:0)

将其用作

select Name, count(Name) as Count from YourTable
group by Name
union 
Select 'SUM' , COUNT(Name) from YourTable

答案 12 :(得分:0)

我正在使用SQL Server,并且以下内容适用于您:

选择强制类型转换(name作为varchar(16))作为'Name',count(name)作为'Count'   从表1   按名称分组   全部合并   选择“总和:”,计数(名称)   从表1

答案 13 :(得分:0)

我还需要having count(*) > 1。因此,我在参考了上述一些查询后写了自己的查询

语法:

select sum(count) from (select count(`table_name`.`id`) as `count` from `table_name` where {some condition} group by {some_column} having count(`table_name`.`id`) > 1) as `tmp`;

示例:

select sum(count) from (select count(`table_name`.`id`) as `count` from `table_name` where `table_name`.`name` IS NOT NULL and `table_name`.`name` != '' group by `table_name`.`name` having count(`table_name`.`id`) > 1) as `tmp`;

答案 14 :(得分:-1)

查询后,运行以下以获取总行数

select @@ROWCOUNT

答案 15 :(得分:-3)

从中选择总和 (选择计数(Col_name)作为来自Tab_name组的s by Col_name具有count(*)&gt; 1)c