Unix Technical Forum

SQL problem - a variation on outer join

This is a discussion on SQL problem - a variation on outer join within the SQL Server forums, part of the Microsoft SQL Server category; --> All, I am far from expert with SQL and have been unable to solve the following problem (stated in ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-14-2008, 06:45 PM
jwwarrenva@gmail.com
 
Posts: n/a
Default SQL problem - a variation on outer join

All,

I am far from expert with SQL and have been unable to solve the
following problem (stated in generic terms):

I have three tables:
1. Employees - contains EmployeeName
2. Years - contains Year
3. Earnings - contains Year, EmployeeName, and Earnings

I need a query that will return a row with the Year, EmployeeName, and
Earnings for every possible combination of Year and EmployeeName, even
if that combination does not appear in the Earnings table.

If it matters, I am using SQL Server 2005.

Can you help me?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-14-2008, 06:45 PM
Plamen Ratchev
 
Posts: n/a
Default Re: SQL problem - a variation on outer join

You need to CROSS JOIN the Employees and Years tables to get all possible
combinations and then do a LEFT JOIN to the Earnings table:

SELECT E.EmployeeName,
Y.[Year],
M.Earnings
FROM Employees AS E
CROSS JOIN Years AS Y
LEFT OUTER JOIN Earnings AS M
ON E.EmployeeName = M.EmployeeName
AND Y.[Year] = M.[Year];

This will show Earnings as NULL for non-matching employee/year. You can use
COALESCE(M.Earnings, 0) if you need it as 0.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-14-2008, 06:45 PM
jwwarrenva@gmail.com
 
Posts: n/a
Default Re: SQL problem - a variation on outer join

On Jul 8, 4:59*pm, "Plamen Ratchev" <Pla...@SQLStudio.com> wrote:
> You need to CROSS JOIN the Employees and Years tables to get all possible
> combinations and then do a LEFT JOIN to the Earnings table:
>
> SELECT E.EmployeeName,
> * * * * * Y.[Year],
> * * * * * M.Earnings
> FROM Employees AS E
> CROSS JOIN Years AS Y
> LEFT OUTER JOIN Earnings AS M
> * ON E.EmployeeName = M.EmployeeName
> *AND Y.[Year] = M.[Year];
>
> This will show Earnings as NULL for non-matching employee/year. You can use
> COALESCE(M.Earnings, 0) if you need it as 0.
>
> HTH,
>
> Plamen Ratchevhttp://www.SQLStudio.com


Thanks, Plamen, that was exactly what I needed.

John Warren
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-14-2008, 06:46 PM
Erland Sommarskog
 
Posts: n/a
Default Re: SQL problem - a variation on outer join

(jwwarrenva@gmail.com) writes:
> I am far from expert with SQL and have been unable to solve the
> following problem (stated in generic terms):
>
> I have three tables:
> 1. Employees - contains EmployeeName
> 2. Years - contains Year
> 3. Earnings - contains Year, EmployeeName, and Earnings
>
> I need a query that will return a row with the Year, EmployeeName, and
> Earnings for every possible combination of Year and EmployeeName, even
> if that combination does not appear in the Earnings table.
>
> If it matters, I am using SQL Server 2005.


SELECT E.EmployeeName, Y.Year, Ea.Earnings
FROM Years Y
CROSS JOIN Employees E
LEFT JOIN Earnings Ea ON Ea.Year = Y.Year
AND Ea.EmployeeName = E.EmployeeName


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:38 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com