1

我有一张我在下面提到的表格,我需要得到一个避免重复的总结果,任何人都可以就如何获得下面提到的结果提供任何帮助或建议,谢谢

ID      name    Total   Used
24     John     5        2
24     John     10       6
27     Peter    20       0
27     Peter    20       5

结果应该是这样的

ID      name    Total   Used
24     John     15       8
27     Peter    40       5
4

6 回答 6

9

看起来你只需要SUM()在两列上使用。GROUP BY在 id 和 name 上也使用 a

SELECT id, name, sum(total) All_total, sum(used) All_used
FROM yourtable
GROUP BY id, name

SQL Fiddle with Demo

GROUP BY字段必须包含您选择的不在聚合函数中的任何其他列,因此对于示例,您idnameGROUP BY.

编辑#1您的查询将是:

SELECT [ID] , name, sum([Total]), sum([Used]), [GUID] 
FROM [table].[dbo].[vw_data] 
GROUP BY [ID], [name], [GUID] 
于 2012-08-29T12:08:51.207 回答
1
select sum(Total), sum(Used), ID, name
from table
group by ID, name;
于 2012-08-29T12:08:59.333 回答
0
select ID,      name,   sum( Total,),  sum( Used)
from  table 
group by Id,name;
于 2012-08-29T12:10:05.667 回答
0

试试这个:

select id,name, sum(Total),sum(used)
from tab
group by id,name 
于 2012-08-29T12:10:06.790 回答
0
SELECT id, name, sum(total) total, sum(used) used  from table GROUP BY id, name
于 2012-08-29T12:10:26.273 回答
0

此问题的 MySql 语法:

CREATE TABLE DUPLICATE (ID,NAME, TOTAL, USED);

INSERT INTO DUPLICATE VALUES(24,'John',5,2);
INSERT INTO DUPLICATE VALUES(24,'John',10,6);
INSERT INTO DUPLICATE VALUES(27,'Peter',20,0);
INSERT INTO DUPLICATE VALUES(27,'Peter',20,5);

SELECT ID, NAME, SUM(TOTAL), SUM(USED) FROM DUPLICATE GROUP BY ID, NAME;
于 2012-08-29T12:14:04.513 回答