We help IT Professionals succeed at work.
Get Started

Help with T-SQL to Self Join a table 3 times

640 Views
Last Modified: 2013-11-10
Hello,
I've spent a lot of time trying to figure it out and look like i've hit a wall, I tried every possible join, union to join a table to get only 1 row with 2 new records

I've attached sample data and the way it should look like in the TXT file


I'm using
SELECT distinct * FROM (
  SELECT A.[NURS_STA]
      ,A.[RM_BED]
      ,A.[MRN]
      ,A.[NAME]
      ,A.[ADM_DATE]
      ,A.[ATN_DR]
      ,A.[LOS]
      ,A.[ACCT_NO]
      ,A.[DX0]
      ,B.[DX0] AS [DX1]
      ,C.[DX0] AS [DX2]
  FROM [Census] A
  LEFT OUTER JOIN [Census] B on A.[MRN] = B.[MRN]  
  LEFT OUTER JOIN [Census] C ON A.[MRN] = C.[MRN]

  )A

Open in new window

, but it joins everything with everything!

any help will be appreciated..

Thanks
sample.txt
Comment
Watch Question
CERTIFIED EXPERT
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
This problem has been solved!
Unlock 2 Answers and 22 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE