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 |
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2005-11-11 : 09:15:56
|
| I have a table, structure like this, 3 columns: (I am using SQL server 2000)Date Qty StatusDay1 100 GoodDay1 50 BetterDay2 250 BetterDay3 200 bestIs there any way I can write an SQL to display like below:Date Qty StatusDay1 100 GoodDay1 50 BetterDay1 0 BestDay2 0 GoodDay2 250 BetterDay2 0 BestDay3 0 GoodDay3 0 BetterDay3 200 BestWhat I want is displaying the 0 for the NULL data. Do I have to use PL/SQL. Thanks very much for any though. |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2005-11-11 : 09:20:22
|
| I think I may need anther dimension table like status, which only holds the data Good, Better and Best |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-11-11 : 09:32:51
|
| Yes. You need to have seperate tableMadhivananFailing to plan is Planning to fail |
 |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2005-11-11 : 10:22:29
|
| Cheers! this is resolved, I'll post the anwser later today as I am very busy at the moment. Thanks Madhivanan! |
 |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2005-11-11 : 12:10:43
|
| This is the table I mentioned, I named it as Orders (I: invoiced, D: Despatched)Date Status Qty1 I 1501 I 2001 I 1002 I 802 D 902 I 1203 D 703 D 260To summarize the qty based on Status and Date, we must have a dimension table for the Status, so I create another table called Status: (I: invoiced, D: Despatched)StatusIDThen I used right outer join on above tables to make sure each date has both ‘I’ and ‘D’ status.We can use following SQL statement to get the desired result:SELECT t2.date, t2.status, CASE WHEN SUM(t1.qty) IS NULL THEN 0 ELSE SUM(t1.qty) END AS QtyFROM Orders t1 RIGHT OUTER JOIN (SELECT DISTINCT Orders.date, Status.status FROM Orders, Status) t2 ON t1.status = t2.status AND t1.date = t2.dateGROUP BY t2.date, t2.statusORDER BY t2.dateHere is the output:Date Status Qty1 D 01 I 3502 D 902 I 2003 D 3303 I 0Cheers! |
 |
|
|
|
|
|
|
|