View previous topic :: View next topic
|
Author |
Message |
shikhagupta33
New User
Joined: 11 Apr 2005 Posts: 7 Location: Bangalore
|
|
|
|
I have a table say:
YEAR QTR SALES
2009 Q1 5000
2009 Q2 6000
2009 Q3 4000
2009 Q4 7000
2010 Q1 9000
2010 Q2 5000
2010 Q3 4000
2010 Q4 8000
I want a result set like
YEAR Q1 Q2 Q3 Q4
2009 5000 6000 4000 7000
2010 9000 5000 4000 8000
What will be the sql query to achieve above result set? |
|
Back to top |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
Code: |
select year,
sum(case when qtr = 'Q1' then sales else 0 end) as q1,
sum(case when qtr = 'Q2' then sales else 0 end) as q2,
sum(case when qtr = 'Q3' then sales else 0 end) as q3,
sum(case when qtr = 'Q4' then sales else 0 end) as q4
from Tab1
group by year |
|
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
thx GuyC |
|
Back to top |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
dbzTHEdinosauer wrote: |
thx GuyC |
urwc.
Didn't want to waste time slowfeeding, because I've seen too many terrible constructs (performance wise) to solve this. |
|
Back to top |
|
|
|