Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rows to Columns

Status
Not open for further replies.

dbljackson

Programmer
Feb 8, 2001
8
US
I have a table that keeps sales by day.

I want to return the results in column fashion. Similar to Cross-Tab.

Example

Select sum(Sales on Mondays), sum(Sales on Tuesdays), ...
where date between ( xxxx) and (xxxx)

Any suggestions most appreciated.

TIA

 
What DB are you using? I could do it in Sybase (and therefore probably MS SQL Server)

Greg.
 
Out of interest, if you have a table sales with 2 columns sales_date and sales_qty, then this will work in Sybase (and probably can be adapted for other servers) ....
Code:
select sum(case when datepart(dw,sales_date)=2 then sales_qty else 0 end) Monday
,
sum(case when datepart(dw,sales_date)=3 then sales_qty else 0 end) Tuesday,
sum(case when datepart(dw,sales_date)=4 then sales_qty else 0 end) Wednesday,
sum(case when datepart(dw,sales_date)=5 then sales_qty else 0 end) Thursday,
sum(case when datepart(dw,sales_date)=6 then sales_qty else 0 end) Friday,
sum(case when datepart(dw,sales_date)=0 then sales_qty else 0 end) Saturday,
sum(case when datepart(dw,sales_date)=1 then sales_qty else 0 end) Sunday
from sales
Greg.
 
Thanks Greg.

Just what I needed.

BTW I'm using SQL Server.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top