jsmile
Life is good
发贴: 127
积分: 52
|
于 2005-01-20 02:25
Thanks floater for your input!
After many hours of researching and trying, I finally found the solution: SELECT Cost_Center AS "Cost Center", SUM(Jan) AS Jan, SUM(Feb) AS Feb , SUM(Mar) AS Mar, SUM(Apr) AS Apr FROM (SELECT Cost_Center, CASE WHEN Month = '01' THEN Cost ELSE 0 END AS Jan, CASE WHEN Month = '02' THEN Cost ELSE 0 END AS Feb, CASE WHEN Month = '03' THEN Cost ELSE 0 END AS Mar, CASE WHEN Month = '04' THEN Cost ELSE 0 END AS Apr FROM dbo.monthly_cost ) monthly_cost GROUP BY Cost_Center
It uses case statement to retrieve the right value into the month and then aggregates the values using sum function combined with group by clause.
It seems odd from the database design point of view in terms of the assignment. But I did learn some powerful SQL statements from the it. I think it was well worth the effect. Maybe it's useful later on for doing some quick reports against the database.
Do I deserve some points?
|