Hello
I've got a little "dilemma", trying to make it "all" happen in one query.
I've attached a little picture...
Three tables:
Table 1 - The user table, stores name and a ID (Primary Key/PK)
Table 2 - Stores the number of phones the person has
Table 3 - Stores a telephone number which refers back to a phone. (Ignore the fact that a phonenumber could be the PK itself, ... due to I just made this scenario up...

)
A user can have multiple phones, a phone can have multiple numbers (in this scenario).
Now I want to create a query which results like the picture shows.
Just all those 3 queries into one bigger query, selecting only one "name" from table 1, selecting all his phones in table 2 and all of those phones' phonenumbers in table 3.

The problem isnt to get values from three tables, but to optimize the outcome, without duplicate values...well, read on... :P
Code:
SELECT DISTINCT tbl1.Name, tbl2.ID, tbl2.Phones
FROM tbl1 INNER JOIN
tbl2 ON tbl1.ID = tbl2.nID AND tbl1.Name = 'Martin'
This results in:
Code:
Martin 1 Nokia
Martin 2 Sony
Martin 4 Ipad
As you can see, Martin has already returned three times, I do not want that, plus I also want to query the third table, without anything repeating at all. (Martin cannot have two Nokia phones, nor two of the same telephone number per phone, but two different phones can have the same number, while a phone can also have multiple different telephone numbers...

lol )
So I basically want it to return on
this format:
Code:
Martin //Martin takes up only one row and one column
1 Nokia //From here on and next three rows, phones are returned
2 Sony
4 Ipad
1 1 123456 //Pk, the phoneID and the phone numbers
2 1 234561
3 2 124411
4 4 145141
Instead SqlServer2005 gives Martin in all the rows or a bunch of "null"...
Thanks in advance, suggestions are more than welcome!

- ManyTimes