Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Mladen.Kasalica
Starting Member
1 Post |
Posted - 2013-03-08 : 02:37:16
|
I have a table like this:value id date32 111 05/15/10 0:0027 111 05/15/10 0:154 111 05/15/10 0:300 111 05/15/10 0:451320 112 05/15/10 0:451369 112 05/15/10 0:301453 112 05/15/10 0:151539 112 05/15/10 0:00971 113 05/15/10 0:00930 113 05/15/10 0:15871 113 05/15/10 0:30823 113 05/15/10 0:45and i need query to sum 'value' for each date, result should look like this:sum date2542 05/15/10 0:002410 05/15/10 0:152244 05/15/10 0:302143 05/15/10 0:45please help |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-08 : 04:25:06
|
DECLARE @Tab TABLE (value INT, id INT, dataVal dateTime)INSERT INTO @TabSELECT 32, 111, '05/15/10 0:00' UNION ALLSELECT 27, 111, '05/15/10 0:15' UNION ALLSELECT 4, 111, '05/15/10 0:30' UNION ALLSELECT 0, 111, '05/15/10 0:45' UNION ALLSELECT 1320, 112, '05/15/10 0:45' UNION ALLSELECT 1369, 112, '05/15/10 0:30' UNION ALLSELECT 1453, 112, '05/15/10 0:15' UNION ALLSELECT 1539, 112, '05/15/10 0:00' UNION ALLSELECT 971, 113, '05/15/10 0:00' UNION ALLSELECT 930, 113, '05/15/10 0:15' UNION ALLSELECT 871, 113, '05/15/10 0:30' UNION ALLSELECT 823, 113, '05/15/10 0:45'--and i need query to sum 'value' for each date, result should look like this:/*sum date2542 05/15/10 0:002410 05/15/10 0:152244 05/15/10 0:302143 05/15/10 0:45*/SELECT SUM(value) sumVal, dataValFROM @tabGROUP BY dataVal--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-08 : 05:39:30
|
i hope date values are having precision only till minutes and doesnt have any seconds part else you need to first cast them to smalldatetime before grouping------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|