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.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Sum of value for each date

Author  Topic 

Mladen.Kasalica
Starting Member

1 Post

Posted - 2013-03-08 : 02:37:16
I have a table like this:

value id date
32 111 05/15/10 0:00
27 111 05/15/10 0:15
4 111 05/15/10 0:30
0 111 05/15/10 0:45
1320 112 05/15/10 0:45
1369 112 05/15/10 0:30
1453 112 05/15/10 0:15
1539 112 05/15/10 0:00
971 113 05/15/10 0:00
930 113 05/15/10 0:15
871 113 05/15/10 0:30
823 113 05/15/10 0:45


and i need query to sum 'value' for each date, result should look like this:

sum date
2542 05/15/10 0:00
2410 05/15/10 0:15
2244 05/15/10 0:30
2143 05/15/10 0:45


please 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 @Tab
SELECT 32, 111, '05/15/10 0:00' UNION ALL
SELECT 27, 111, '05/15/10 0:15' UNION ALL
SELECT 4, 111, '05/15/10 0:30' UNION ALL
SELECT 0, 111, '05/15/10 0:45' UNION ALL
SELECT 1320, 112, '05/15/10 0:45' UNION ALL
SELECT 1369, 112, '05/15/10 0:30' UNION ALL
SELECT 1453, 112, '05/15/10 0:15' UNION ALL
SELECT 1539, 112, '05/15/10 0:00' UNION ALL
SELECT 971, 113, '05/15/10 0:00' UNION ALL
SELECT 930, 113, '05/15/10 0:15' UNION ALL
SELECT 871, 113, '05/15/10 0:30' UNION ALL
SELECT 823, 113, '05/15/10 0:45'

--and i need query to sum 'value' for each date, result should look like this:
/*sum date
2542 05/15/10 0:00
2410 05/15/10 0:15
2244 05/15/10 0:30
2143 05/15/10 0:45*/
SELECT SUM(value) sumVal, dataVal
FROM @tab
GROUP BY dataVal


--
Chandu
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -