Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Adding Spouse to Output

Author  Topic 

lindyholic
Starting Member

2 Posts

Posted - 2010-10-07 : 18:56:43
Hi there.

I'm trying to write an SQL Query in my DB program to include the spouse on the same line as the main contact. The spouse holds a seperate table. Essentially, I want the church name, family name, main contact name, spouse name, address, etc. I can get all of this with the spouse taking up the second line, but I want the spouse name included.

What I am getting right now

Parish Name, Last Name, First Name, Address, etc. (Main contact)
Parish Name, Last Name, First Name, Address, etc. (Spouse)

What I want is

Parish Name, Last Name, First Name (main contact), First Name (spouse), Address, etc.

Here is the code I put in for the first result:

SELECT
Parish.C_name, House.family_nam, people.first_name, house.address1, house.address2, house.city, house.prov, house.postal_cod, house.housphone1
FROM
people, house, parish
WHERE
Record_Lev < 3 AND house.family_id = people.family_id

I appreciate the help.

-Harrison

lindyholic
Starting Member

2 Posts

Posted - 2010-10-07 : 19:30:15
Record_Lev is how contacts are distinguished. Where Record_Lev = 1, that is the main contact. Where Record_Lev = 2, that is the spouse.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-07 : 19:48:06
Do a self join to the table:

FROM people p1
JOIN people p2
ON p1.LinkingColumn = p2.LinkingColumn

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -