SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sum on fields between 2 dates


sum on fields between 2 dates

Author
Message
mick burden
mick burden
Old Hand
Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)

Group: General Forum Members
Points: 391 Visits: 431
I want to be able to add together values from fields that come between 2 dates, I've created a table script below

    
IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
DROP TABLE #mytable
CREATE TABLE mytable
(
DateValue DATETIME,
sName varchar(50),
sinterventions INT,
sareas INT )

-- Inserting Data into Table
INSERT INTO mytable(DateValue,sName,sinterventions,sareas) VALUES ('23/Dec/2012','Mike',99,23)
INSERT INTO mytable(DateValue,sName,sinterventions,sareas) VALUES ('22/Dec/2012','Joe',43,3)
INSERT INTO mytable(DateValue,sName,sinterventions,sareas) VALUES ('20/Dec/2012','Tony',5,66)
INSERT INTO mytable(DateValue,sName,sinterventions,sareas) VALUES ('21/Dec/2012','Mike',11,44)
INSERT INTO mytable(DateValue,sName,sinterventions,sareas) VALUES ('20/Dec/2012','sally',1,440)
INSERT INTO mytable(DateValue,sName,sinterventions,sareas) VALUES ('21/Dec/2012','sally',11,20)
INSERT INTO mytable(DateValue,sName,sinterventions,sareas) VALUES ('20/Nov/2012','Joy',121,70)




What I'd like to return is if I query the data between 20/dec/2012 and 23/Dec/2012,


Mike 110 67
Joe 43 3
Tony 5 66
Sally 12 460

I've tried GroupBy but that just splits it over dates, so I'll get 2 Sally's etc. How could this be achieved?
Chris Harshman
Chris Harshman
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4980 Visits: 4000
seems simple enough, your dates should be in the WHERE clause, not GROUP BY clause, like this:
SELECT sName, SUM(sinterventions) AS sinterventions, SUM(sareas) AS sareas
FROM mytable
WHERE DateValue BETWEEN '2012-12-20' AND '2012-12-23'
GROUP BY sName


mick burden
mick burden
Old Hand
Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)

Group: General Forum Members
Points: 391 Visits: 431
Thanks Chris, I must admit I tried everything except that yesterday.. that worked a treat
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search