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 |
micnie_2020
Posting Yak Master
232 Posts |
Posted - 2014-07-02 : 03:40:11
|
Hi All,I have a table stored Dates-----01/Apr/201401/May/201401/Jun/201401/Jul/2014I have data Data table-----------------------ID Description Dates Amount 2 A 01/Apr/2014 500 3 B 01/Apr/2014 3003 B 01/May/2014 2004 T 01/Apr/2014 6004 T 01/May/2014 7004 T 01/Jun/2014 8004 T 01/Jul/2014 500I want output to be:-----------------------------ID Description Dates Amount 2 A 01/Apr/2014 500 2 A 01/May/2014 0 <<New added2 A 01/Jun/2014 0 2 A 01/Jul/2014 0 <<new added 3 B 01/Apr/2014 3003 B 01/May/2014 2003 B 01/Jun/2014 0 <<new added 3 B 01/Jul/2014 0 <<new added 4 T 01/Apr/2014 6004 T 01/May/2014 7004 T 01/Jun/2014 8004 T 01/Jul/2014 500Please advise.Thank you. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-07-02 : 04:13:12
|
[code]SELECT x.ID, x.[Description], d.Dates, ISNULL(w.Amount, 0) AS AmountFROM dbo.Dates AS dCROSS JOIN ( SELECT DISTINCT ID, [Description] FROM dbo.Data ) AS xLEFT JOIN dbo.Data AS w ON w.ID = x.ID AND w.[Description] = x.[Description] AND w.Dates = d.Dates;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|