我有一个包含 2 个字段的表:
身份证名称 -- ------ 1 阿尔法 2 测试版 3 测试版 4 测试版 5 查理 6查理
我想按名称对它们进行分组,使用“count”和一行“SUM”
名称计数 -------- ----- 阿尔法 1 测试版 3 查理 2 总和 6
我将如何编写查询以在表下方添加 SUM 行?
我有一个包含 2 个字段的表:
身份证名称 -- ------ 1 阿尔法 2 测试版 3 测试版 4 测试版 5 查理 6查理
我想按名称对它们进行分组,使用“count”和一行“SUM”
名称计数 -------- ----- 阿尔法 1 测试版 3 查理 2 总和 6
我将如何编写查询以在表下方添加 SUM 行?
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
SELECT name, COUNT(name) AS count, SUM(COUNT(name)) OVER() AS total_count
FROM Table GROUP BY name
没有指定您正在使用哪个 rdbms
看看这个演示
SELECT Name, COUNT(1) as Cnt
FROM Table1
GROUP BY Name
UNION ALL
SELECT 'SUM' Name, COUNT(1)
FROM Table1
也就是说,我建议您的表示层而不是数据库添加总数。
这更像是使用 ROLLUP 汇总数据的 SQL SERVER 版本
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
尝试这个:
SELECT ISNULL(Name,'SUM'), count(*) as Count
FROM table_name
Group By Name
WITH ROLLUP
这里的所有解决方案都很棒,但不一定可以为旧的 mysql 服务器实现(至少在我的情况下)。所以你可以使用子查询(我认为它不那么复杂)。
select sum(t1.cnt) from
(SELECT column, COUNT(column) as cnt
FROM
table
GROUP BY
column
HAVING
COUNT(column) > 1) as t1 ;
请按以下方式运行:
Select sum(count)
from (select Name,
count(Name) as Count
from YourTable
group by Name); -- 6
我解释这个问题的方式是需要每组答案的小计值。小计结果非常容易,使用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
您可以使用联合来连接行。
select Name, count(*) as Count from yourTable group by Name
union all
select "SUM" as Name, count(*) as Count from yourTable
对于 Sql server 你可以试试这个。
SELECT ISNULL([NAME],'SUM'),Count([NAME]) AS COUNT
FROM TABLENAME
GROUP BY [NAME] WITH CUBE
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
您可以使用 ROLLUP
select nvl(name, 'SUM'), count(*)
from table
group by rollup(name)
您可以尝试按名称分组并计算该组中的 ID。
SELECT name, count(id) as COUNT FROM table group by name
用它作为
select Name, count(Name) as Count from YourTable
group by Name
union
Select 'SUM' , COUNT(Name) from YourTable
我正在使用 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
我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`;
查询后,在下面运行以获取总行数
select @@ROWCOUNT
select sum(s) from (select count(Col_name) as s from Tab_name group by Col_name with count(*)>1)c