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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-02-28 : 07:44:29
|
Pelle writes "Hi,I have a table with three fields: product, quantity and a dateI would like to write a question thats shows the quantity for each product summarized per month.If the data looks like this:product quantity date------------------------------------prod1 100 2005-01-01prod1 50 2005-01-01prod1 70 2005-02-01prod2 10 2005-02-02 I would like the result to look like this, where the columns represent month 1 - 12prod1 150 70 0 0 0 0 0 0 0 0 0 0prod2 0 10 0 0 0 0 0 0 0 0 0 0 Thanks!Pelle" |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-02-28 : 11:36:28
|
| [code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(product varchar(15), quantity int, [date] datetime)GOINSERT INTO myTable99(product, quantity, [date])SELECT 'prod1', 100, '2005-01-01' UNION ALLSELECT 'prod1', 50, '2005-01-01' UNION ALLSELECT 'prod1', 70, '2005-02-01' UNION ALLSELECT 'prod2', 10, '2005-02-02' GOSELECT Product , SUM(JAN) AS SUM_JAN , SUM(FEB) AS SUM_FEB-- , ect FROM (SELECT Product , CASE WHEN DATEPART(mm,[date]) = 1 THEN quantity ELSE 0 END AS JAN , CASE WHEN DATEPART(mm,[date]) = 2 THEN quantity ELSE 0 END AS FEB-- , ect FROM myTable99) AS XXXGROUP BY ProductGODROP TABLE myTable99GO[/code]Brett8-) |
 |
|
|
|
|
|
|
|