SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Sum of value for each date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Mladen.Kasalica
Starting Member

1 Posts

Posted - 03/08/2013 :  02:37:16  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 03/08/2013 :  04:25:06  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 03/08/2013 :  05:39:30  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000