82

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

身份证名称
-- ------
1 阿尔法
2 测试版
3 测试版
4 测试版
5 查理
6查理

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

名称计数
-------- -----
阿尔法 1
测试版 3
查理 2
总和 6

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

4

17 回答 17

93
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
于 2012-10-17T04:50:56.180 回答
79
SELECT name, COUNT(name) AS count, SUM(COUNT(name)) OVER() AS total_count
FROM Table GROUP BY name
于 2015-07-15T13:43:52.010 回答
17

没有指定您正在使用哪个 rdbms

看看这个演示

SQL 小提琴演示

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

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

这更像是使用 ROLLUP 汇总数据的 SQL SERVER 版本

SQL 小提琴演示

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
于 2012-10-17T04:50:54.513 回答
10

尝试这个:

SELECT  ISNULL(Name,'SUM'), count(*) as Count
FROM table_name
Group By Name
WITH ROLLUP
于 2012-10-17T05:10:35.397 回答
4

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

 select sum(t1.cnt) from 
        (SELECT column, COUNT(column) as cnt
            FROM
            table 
            GROUP BY 
            column
            HAVING 
            COUNT(column) > 1) as t1 ;
于 2020-02-26T06:17:41.817 回答
4

请按以下方式运行:

Select sum(count) 
  from (select Name, 
               count(Name) as Count 
          from YourTable
      group by Name);  -- 6
于 2016-03-21T16:09:34.187 回答
3

我解释这个问题的方式是需要每组答案的小计值。小计结果非常容易,使用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
于 2020-06-08T20:29:42.793 回答
2

您可以使用联合来连接行。

select Name, count(*) as Count from yourTable group by Name
union all
select "SUM" as Name, count(*) as Count from yourTable
于 2012-10-17T04:51:55.013 回答
2

对于 Sql server 你可以试试这个。

SELECT ISNULL([NAME],'SUM'),Count([NAME]) AS COUNT
FROM TABLENAME
GROUP BY [NAME] WITH CUBE
于 2015-07-15T12:20:16.013 回答
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
于 2016-10-24T06:50:59.583 回答
1

您可以使用 ROLLUP

select nvl(name, 'SUM'), count(*)
from table
group by rollup(name)
于 2016-11-02T17:32:19.290 回答
0

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

SELECT name, count(id) as COUNT FROM table group by name
于 2012-10-17T04:50:05.853 回答
0

用它作为

select Name, count(Name) as Count from YourTable
group by Name
union 
Select 'SUM' , COUNT(Name) from YourTable
于 2012-10-17T04:58:01.360 回答
0

我正在使用 SQL 服务器,以下内容应该适合您:

select cast(name as varchar(16)) as 'Name', count(name) as 'Count' from Table1 group by Name union all select 'Sum:', count(name) from Table1

于 2018-12-14T19:11:16.713 回答
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`;
于 2020-01-08T12:34:50.617 回答
-2

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

select @@ROWCOUNT
于 2016-01-21T05:04:20.187 回答
-6

select sum(s) from (select count(Col_name) as s from Tab_name group by Col_name with count(*)>1)c

于 2016-10-24T07:45:35.017 回答