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 2008 Forums
 Transact-SQL (2008)
 Grouping Columns

Author  Topic 

KingCarlos
Yak Posting Veteran

74 Posts

Posted - 2012-09-24 : 01:14:45
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)

17689 Posts

Posted - 2012-09-24 : 02:01:25
[code]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 [/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -