(jwwarrenva@gmail.com) writes:
Quote:
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