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 |
thanksfor help
Posting Yak Master
106 Posts |
Posted - 2013-10-05 : 03:52:59
|
Hi,I have 2 table depart and wk_sales need to full outer join to get all the depart even when there is no sales for that week.Wk_saleswk_1-----dept1------------1000wk_1-----dept2------------2000wk_1-----dept3------------3000wk_2-----dept1------------2000wk_2-----dept4------------1000wk_3-----dept2------------2000wk_3-----dept3------------2000wk_3-----dept4------------1000deptd1------dept1d2------dept2d3------dept3d4------dept4d5------dept5Query resultwk_1-----dept1------------1000wk_1-----dept2------------2000wk_1-----dept3------------3000wk_1-----dept4------------0wk_1-----dept5------------0wk_2-----dept1------------2000wk_2-----dept2------------0wk_2-----dept3------------0wk_2-----dept4------------1000wk_2-----dept5------------0Wk3----------------------------Select * from WK_sales sfull outer join dept d on s.dept_code = d.dept_codeonly gives extra row for dept5 in the end.But I need every dept for each week.any suggestion will be helpfullthanks |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-10-05 : 08:47:59
|
[code]SELECT d.Dept, w.Wk, COALESCE(q.Sales, 0) AS SalesFROM dbo.Dept AS dCROSS JOIN ( SELECT Wk FROM dbo.Wk_Sales GROUP BY Wk ) AS wLEFT JOIN dbo.Wk_Sales AS q ON q.Dept = d.Dept AND q.Wk = w.Wk[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|