Avatar of TadSter
 asked on

How to join queries

I can't figure this out. What I have is

Folder      Client      Name      PrimarySpouse
1002      1002      John Delight      1
1002      1002      Jane Delight      0
1024      1024      Peter Sheffen      1
2056      2056      Rock Smith      1
2056      2056      Rachel Smith      0
2056      4056      Child Smith      1

What I want is 3 columns with
Client      PrimarySpouseName      SecondSpouseName

To exclude chidren I use Where L.Client = L.Folder

This join doesn't work
SELECT TOP 100 L.client
FROM [NamAddr].[dbo].[tbClients] as L
Where L.Client = L.Folder and L.PrimarySpouse = 1
Join [NamAddr].[dbo].[tbClients] as R
On L.Client = R.Client
Where R.Client = R.Folder and R.PrimarySpouse = 0
Order by Client

How do I create a join with the wife on the same record as the husband?
Microsoft SQL Server

Avatar of undefined
Last Comment

8/22/2022 - Mon
Kent Dyer

Wait..  What about UNION?  I think that is what you are looking for..

If memory serves, MS SQL wants to use a INNER JOIN..  You also have LEFT and RIGHT joins as well.



Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Wow! that's nice. But I also wanted the single Peter Sheffen in there. I am not sure how to modify the query to include him.
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Thank you so much for your help. I am grateful!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck