Re: getting sum of units sold in preceeding weeks
From: Vishal Parkar (REMOVE_THIS_vgparkar_at_yahoo.co.in)
Date: 05/11/04
- Next message: Vishal Parkar: "Re: can a column be named as a variable?"
- Previous message: Vishal Parkar: "Re: Third largest salary"
- In reply to: Nikhil: "Re: getting sum of units sold in preceeding weeks"
- Next in thread: Nikhil: "Re: getting sum of units sold in preceeding weeks"
- Reply: Nikhil: "Re: getting sum of units sold in preceeding weeks"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 11 May 2004 21:06:34 +0530
>>can i have the same in diff columns...e.g. the table column heading would
be
yes possible, you only have to add group by clause. See following example
--sample data
create table tableA
(dt_column datetime,
sold_units int,
itemid int)
insert into tableA values(getdate() ,3, 1)
insert into tableA values(getdate() -1 ,3, 1)
insert into tableA values(getdate() -15,3, 1)
insert into tableA values(getdate() ,3, 2)
insert into tableA values(getdate() -15, 43, 2)
--required query
select itemid,sum(case when dt_column >= dateadd(dd, -7, {fn
current_date()} )
and dt_column <= getdate() then sold_units else 0 end) 'week1',
sum(case when dt_column >= dateadd(dd, -15, {fn current_date()} ) and
dt_column < dateadd(dd, -7, {fn current_date()} ) then sold_units else 0
end) 'week2'
from
tableA
group by itemid
-- Vishal Parkar vgparkar@yahoo.co.in | vgparkar@hotmail.com
- Next message: Vishal Parkar: "Re: can a column be named as a variable?"
- Previous message: Vishal Parkar: "Re: Third largest salary"
- In reply to: Nikhil: "Re: getting sum of units sold in preceeding weeks"
- Next in thread: Nikhil: "Re: getting sum of units sold in preceeding weeks"
- Reply: Nikhil: "Re: getting sum of units sold in preceeding weeks"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|