views:

46

answers:

1

Can someone explain the statement below to me with a working sample/example. thanks in advance.

You can not create duplicate fields, but simply add a single extra field, "coupleId", which would have a unique id for each couple; and two rows (one for each person) per couple; then JOIN the table against itself with a constraint like a.coupleId = b.coupleId AND a.id <> b.id so that you can condense the data into a single result row for a given couple.

A: 

Given the table Person, as follows:

Person
==========
PersonID (int, autoincrement)
CoupleID (nullable int)
Name (nullable varchar(50))

Insert some rows:

insert into Person (CoupleID, Name) values (1, 'John')
insert into Person (CoupleID, Name) values (1, 'Mary')
insert into Person (CoupleID, Name) values (null, 'Sue')

Now you can find people that make up a couple with the following query:

select p1.CoupleID, p1.PersonID as Person1ID, p1.Name as Person1Name, p2.PersonID as Person2ID, p2.Name as Person2Name 
from Person p1 
inner join Person p2 on p1.CoupleID = p2.CoupleID 
where p1.PersonID <> p2.PersonID 
order by p1.PersonID

Output:

PersonID    CoupleID    Name                                               PersonID    CoupleID    Name
----------- ----------- -------------------------------------------------- ----------- ----------- --------------------------------------------------
2           1           Mary                                               1           1           John
1           1           John                                               2           1           Mary

(2 row(s) affected)
RedFilter
thanks for the help. I'll have 9 other fields such as Age, Height, Weight, etc... for a couple each would have to be specific, so would I have to add Age1, Age2, Height1, Height2, etc...? If I have to do that would it make more since to create a CoupleTbl and put the main (first) person in the MainMember table and the second person in the CoupleTbl and use the PersonID and CoupleID... and just have it check to see if CoupleID is null and if not go to CoupleTBL and match the PersonID and grab the second persons data?
acctman
@acctaman, what is best for you depends on the usage and the meaning of your data. read about normalization if you can. think about reporting. iterate.
Unreason