Contando duplicados de la tabla
Frecuentes
Visto 135 veces
1
I have got a table which i have mentioned below, i need to get a total results avoiding duplicates , can any one provide any assistance or suggestions on how to get the results mentioned below, thankyu
ID name Total Used
24 John 5 2
24 John 10 6
27 Peter 20 0
27 Peter 20 5
Result should be something like this
ID name Total Used
24 John 15 8
27 Peter 40 5
6 Respuestas
9
Looks like you just need to use SUM()
on the two columns. Also use a GROUP BY
on the id and name
SELECT id, name, sum(total) All_total, sum(used) All_used
FROM yourtable
GROUP BY id, name
ver Violín SQL con demostración
La GROUP BY
field must include any other columns you are selecting which are not in an aggregate function, so for the example you would include both id
y name
en la GROUP BY
.
Edit #1 your query would be:
SELECT [ID] , name, sum([Total]), sum([Used]), [GUID]
FROM [table].[dbo].[vw_data]
GROUP BY [ID], [name], [GUID]
respondido 08 nov., 12:21
1
select sum(Total), sum(Used), ID, name
from table
group by ID, name;
Respondido 29 ago 12, 13:08
0
select ID, name, sum( Total,), sum( Used)
from table
group by Id,name;
Respondido 29 ago 12, 13:08
0
Prueba esto:
select id,name, sum(Total),sum(used)
from tab
group by id,name
Respondido 29 ago 12, 13:08
0
SELECT id, name, sum(total) total, sum(used) used from table GROUP BY id, name
Respondido 29 ago 12, 13:08
0
MySql syntax for this problem:
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;
Respondido 29 ago 12, 13:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas asp.net sql sql-server tsql or haz tu propia pregunta.
tried the above query but getting this error: Column...is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause - Mr A
@MrA are you adding any additional fields, if so then you will need to include them in the
GROUP BY
- Taryn@MrA - Please don't cut bits out of error messages. What does the whole error say? And are you running exactly the same query or have you modified it? - MatBailie
@MrA The columns in the select list, which are not using any aggregate function must be present in the group by clause, Add all of your columns in the Group By clause who are not using Sum or any function. - Waqar Janjuá
still getting error , thats my final query: SELECT [ID] ,sum[name] ,sum[Total] ,[Used] ,[GUID] FROM [table].[dbo].[vw_data] GROUP BY [ID] , [name] , [GUID] - Mr A