SQL JOIN help?

Hello,

I am trying to figure out how to query 2 tables to get desired results.  The problem I am having is that the same LabName(description) is being returned for all 4 Activity Numbers instead of the corresponding LabName for each Activity Number for each row.

Current query:
SELECT RI.Location, AC.Description AS Lab1, RI.Activity1, (RI.Cost1 * 31) AS Cost1, AC.Description AS Lab2, RI.Activity2, (RI.Cost2 * 31) AS Cost2,
AC.Description AS Lab3, RI.Activity3, (RI.Cost3 * 31) AS Cost3, AC.Description AS Lab4, RI.Activity4, (RI.Cost4 * 31) AS Cost4,
 ((RI.Cost1 + RI.Cost2 + RI.Cost3 + RI.Cost4) * 31) AS TCost

FROM [SeedTracker].[dbo].[DBLogResInt] AS RI

JOIN [SeedTracker].[dbo].[ActiveCodes] AS AC
ON (RI.Activity1 = AC.ActivityCode)

ORDER BY Location

Open in new window

Results:  results
[DBLogResInt]  DBLogResInt
[ActivityCodes]  [ActivityCodes
AhelblingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
Here it goes. You need to repeat the AC table as many times as you want descriptions.
SELECT ri.location
     , ac1.description AS lab1
     , ri.activity1
     , (ri.cost1 * 31) AS cost1
     , ac2.description AS lab2
     , ri.activity2
     , (ri.cost2 * 31) AS cost2
     , ac3.description AS lab3
     , ri.activity3
     , (ri.cost3 * 31) AS cost3
     , ac4.description AS lab4
     , ri.activity4
     , (ri.cost4 * 31) AS cost4
     , ((ri.cost1 + ri.cost2 + ri.cost3 + ri.cost4) * 31) AS tcost
  FROM [seedtracker].[dbo].[DBLogResInt] AS RI
JOIN [SeedTracker].[dbo].[ActiveCodes] AS AC1
ON (RI.Activity1 = AC1.ActivityCode)
JOIN [SeedTracker].[dbo].[ActiveCodes] AS AC2
ON (RI.Activity2 = AC2.ActivityCode)
JOIN [SeedTracker].[dbo].[ActiveCodes] AS AC3
ON (RI.Activity3 = AC3.ActivityCode)
JOIN [SeedTracker].[dbo].[ActiveCodes] AS AC4
ON (RI.Activity4 = AC4.ActivityCode)
ORDER BY Location

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AhelblingAuthor Commented:
Hmmm, that didn't seem to work.
results2.JPG
0
Walter RitzelSenior Software EngineerCommented:
Just change the join to left join
SELECT ri.location
     , ac1.description AS lab1
     , ri.activity1
     , (ri.cost1 * 31) AS cost1
     , ac2.description AS lab2
     , ri.activity2
     , (ri.cost2 * 31) AS cost2
     , ac3.description AS lab3
     , ri.activity3
     , (ri.cost3 * 31) AS cost3
     , ac4.description AS lab4
     , ri.activity4
     , (ri.cost4 * 31) AS cost4
     , ((ri.cost1 + ri.cost2 + ri.cost3 + ri.cost4) * 31) AS tcost
  FROM [seedtracker].[dbo].[DBLogResInt] AS RI
LEFT JOIN [SeedTracker].[dbo].[ActiveCodes] AS AC1
ON (RI.Activity1 = AC1.ActivityCode)
LEFT JOIN [SeedTracker].[dbo].[ActiveCodes] AS AC2
ON (RI.Activity2 = AC2.ActivityCode)
LEFT JOIN [SeedTracker].[dbo].[ActiveCodes] AS AC3
ON (RI.Activity3 = AC3.ActivityCode)
LEFT JOIN [SeedTracker].[dbo].[ActiveCodes] AS AC4
ON (RI.Activity4 = AC4.ActivityCode)
ORDER BY Location

Open in new window

0
AhelblingAuthor Commented:
That did it!  Thank you !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.