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 2008 Forums
 Transact-SQL (2008)
 Grouping Columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

KingCarlos
Yak Posting Veteran

Australia
74 Posts

Posted - 09/24/2012 :  01:14:45  Show Profile  Reply with Quote
I have the following SQL queries that give the qty per code over a particular date range.

How do I get this into a sinlge data set with the different time ranges next to each other?

select sum (quantity), code from stock
where ddate between dateadd(day, datediff(day, 0, getdate()), -91)
and getdate() group by code
go
select sum (quantity), code from stock
where ddate between dateadd(day, datediff(day, 0, getdate()), -182)
and dateadd(day, datediff(day, 0, getdate()), -91) group by code
go

Any advice is always welcomed.

khtan
In (Som, Ni, Yak)

Singapore
17650 Posts

Posted - 09/24/2012 :  02:01:25  Show Profile  Reply with Quote
select sum (case when ddate between dateadd(day, datediff(day, 0, getdate()), -182)
                            and     dateadd(day, datediff(day, 0, getdate()), -91)  then quantity else 0 end), 
       sum (case when ddate between dateadd(day, datediff(day, 0, getdate()), -91)
                            and     getdate() then quantity else 0 end), 
       code 
from   stock
where  ddate between dateadd(day, datediff(day, 0, getdate()), -182)
             and     getdate()
group by code



KH
Time is always against us

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.16 seconds. Powered By: Snitz Forums 2000