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 |
|
pavlos
Yak Posting Veteran
59 Posts |
Posted - 2011-12-15 : 19:11:28
|
| hey all,i am having trouble coding something at work and sql isn't really my forte.. don't even know what to search for to find a correct solution.to make it simple for you.I need to present a table which consists ofStore name | Monthly Target | Target to the current dateSo:lets say I have a database called 'massive' with the store name = store, date = 2011-01-01, targetday = $xxxxI have gone like thisselect store, sum(targetday) from massivewhere date between '2011-01-01' and '2011-01-31'group by storethis will return the store name and target for the whole month but I cant add the total target to a current day as I am already using a particular sum.I also want to return the sum(targetday) on for the days '2011-01-01' to '2011-01-10' - say that the 10th was the present daySo do I need to combine SQL statements?I was thinking something like this (obviously this doesnt run)Select store, (select sum(targetdate) from massive where date between '2011-01-01' and '2011-01-05') AS 'Month to day target', sum(targetday) from massivewhere date between '2011-01-01' and '2011-01-31'group by storei hope this makes senseeven if you guys could point me in the direction of a good example that would be fantastic.. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-15 : 19:35:18
|
[code]select store, sum(targetday) as whole_month, sum(case when date between '2011-01-01' and '2011-01-10' then targetday else 0 end)from massivewhere date between '2011-01-01' and '2011-01-31'group by store[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-12-15 : 19:35:52
|
Would this work for you?SELECT store, SUM(targetday), SUM(CASE WHEN date BETWEEN '2011-01-01' AND '2011-01-10' THEN targetday END) AS TargetToJan10thFROM massiveWHERE date BETWEEN '2011-01-01' AND '2011-01-31'GROUP BY store |
 |
|
|
pavlos
Yak Posting Veteran
59 Posts |
Posted - 2011-12-15 : 19:37:38
|
| SELECT StoreName, (SELECT SUM(Amount) FROM BUDGET_LINES WHERE Date between '2011-01-01' and '2011-01-31') AS 'Mothly Target', (SELECT SUM(Amount) FROM BUDGET_LINES WHERE Date between '2011-01-01' and '2011-01-10') AS 'Target to day'FROM BUDGET_LINESGROUP BY StoreName |
 |
|
|
pavlos
Yak Posting Veteran
59 Posts |
Posted - 2011-12-15 : 19:38:11
|
| hopefully the post above this one makes more sense on what i am trying to achieve.. cheers |
 |
|
|
pavlos
Yak Posting Veteran
59 Posts |
Posted - 2011-12-15 : 19:42:35
|
| sorry guys, didn't see the replies... khtan... your response worked.. thanks again!!!! |
 |
|
|
|
|
|
|
|