Returning multiple cross referenced values.

There has to be a better way to do this?
This query returns four records, two of which are incomplete. I need to return a single complete record

 select
     isnull(t1.[EMAIL], '') [Safety_Email]
    ,isnull(t2.[EMAIL], '') [Eng_Email]
    ,isnull(t3.[EMAIL], '') [Shop_Email]
    ,isnull(t4.[EMAIL], '') [Fore_Email]
    ,isnull(t5.[EMAIL], '') [Qc_Email]
    ,isnull(t6.[EMAIL], '') [Drft_Email]
    ,isnull(t7.[EMAIL], '') [Mgr_Email]
    ,isnull(t8.[EMAIL], '') [Parts_Email]
    ,isnull(t9.[EMAIL], '') [Ship_Email]
  from [WorkOrder] w
  left outer join [EMP_MST] t1 on w.[SAFETY_INI] = t1.[INITIALS]
  left outer join [EMP_MST] t2 on w.[ENG_INI]    = t2.[INITIALS]
  left outer join [EMP_MST] t3 on w.[SHOP_INI]   = t3.[INITIALS]
  left outer join [EMP_MST] t4 on w.[FORE_INI]   = t4.[INITIALS]
  left outer join [EMP_MST] t5 on w.[QC_INI]     = t5.[INITIALS]
  left outer join [EMP_MST] t6 on w.[DRFT_INI]   = t6.[INITIALS]
  left outer join [EMP_MST] t7 on w.[MGR_INI]    = t7.[INITIALS]
  left outer join [EMP_MST] t8 on w.[PARTS_INI]  = t8.[INITIALS]
  left outer join [EMP_MST] t9 on w.[SHIP_INI]   = t9.[INITIALS]
  where [JOB_NUM] = 115390

Thank you in advance,

Joe
LVL 4
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerAsked:
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.

Scott PletcherSenior DBACommented:
Try this:
select 
     max(case when em.[INITIALS] = w.[SAFETY_INI] then em.[EMAIL] end) AS [Safety_Email],
     MAX(case when  em.[INITIALS] = w.[ENG_INI] then em.[EMAIL] end) AS [Eng_Email],
     --...
     MAX(case when  em.[INITIALS] = w.[SHIP_INI] then em.[EMAIL] end) AS [Ship_Email]
  from [WorkOrder] w
  left outer join [EMP_MST] em on
      em.[INITIALS] IN (w.[SAFETY_INI], w.[ENG_INI], w.[SHOP_INI],
          w.[FORE_INI], w.[QC_INI], w.[DRFT_INI],
          w.[MGR_INI], w.[PARTS_INI], w.[SHIP_INI])
  where [JOB_NUM] = 115390
  group by [JOB_NUM]

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
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerAuthor Commented:
As always, Thank you Scott!
I was having one of those metal block days.
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 2005

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.