![Antiguo](http://static.forosdelweb.com/fdwtheme/images/statusicon/post_old.gif)
14/11/2011, 09:07
|
![Avatar de Libras](http://static.forosdelweb.com/customavatars/avatar147802_5.gif) | Colaborador | | Fecha de Ingreso: agosto-2006 Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 5 meses Puntos: 774 | |
Respuesta: Sumar valores en base a otra columna
Código SQL:
Ver originalCREATE TABLE #temp( categoria VARCHAR(20), marca VARCHAR(10), sku VARCHAR(5), volumen INT ) INSERT INTO #temp VALUES ('Vinagre', 'Bonna', '0,5', 25) INSERT INTO #temp VALUES ('Vinagre', 'Bonna', '1', 32) INSERT INTO #temp VALUES ('Vinagre', 'Casa', '0,5', 47) INSERT INTO #temp VALUES ('Vinagre', 'Casa', '1', 12) INSERT INTO #temp VALUES ('Vinagre', 'Casa', '2', 18) INSERT INTO #temp VALUES ('Vinagre', 'Mazeite', '0,75', 31) INSERT INTO #temp VALUES ('Vinagre', 'Mazeite', '1', 44) INSERT INTO #temp VALUES ('Vinagre', 'Mazeite', '2', 20) INSERT INTO #temp VALUES ('Vinagre', 'Vatel','1', 60) INSERT INTO #temp VALUES ('Vinagre', 'Vatel','2', 10) SELECT SUM(volumen) AS suma, sku INTO #temp2 FROM #temp GROUP BY sku UPDATE #temp SET volumen=t1.suma FROM ( SELECT suma, sku FROM #temp2 ) AS t1 WHERE #temp.sku=t1.sku SELECT * FROM #temp
Resultado:
categoria marca sku volumen
Vinagre Bonna 0,5 72
Vinagre Bonna 1 148
Vinagre Casa 0,5 72
Vinagre Casa 1 148
Vinagre Casa 2 48
Vinagre Mazeite 0,75 31
Vinagre Mazeite 1 148
Vinagre Mazeite 2 48
Vinagre Vatel 1 148
Vinagre Vatel 2 48
Saludos!
__________________ What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me |