If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > One query against three tables... Return what I want.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-08-10, 20:34
ManyTimes ManyTimes is offline
Registered User
 
Join Date: Jul 2010
Location: Norway
Posts: 16
One query against three tables... Return what I want.

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
Attached Thumbnails
One query against three tables... Return what I want.-helpsql.jpg  

Last edited by ManyTimes; 07-08-10 at 20:42.
Reply With Quote
  #2 (permalink)  
Old 07-09-10, 04:49
IsMonkey IsMonkey is offline
Registered User
 
Join Date: Jul 2010
Posts: 6
it might help you...
[code]
Select from tb1 a inner join(tbl2 b inner join tb3 on b.pid=cpid) on a.nid=b.nid
[\code]
Reply With Quote
  #3 (permalink)  
Old 07-09-10, 04:51
IsMonkey IsMonkey is offline
Registered User
 
Join Date: Jul 2010
Posts: 6
you need to have an identity on each table and link it.. use the referential integrity...
Reply With Quote
  #4 (permalink)  
Old 07-09-10, 05:09
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
You cannot do it. You must return three result sets.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #5 (permalink)  
Old 07-09-10, 07:34
ManyTimes ManyTimes is offline
Registered User
 
Join Date: Jul 2010
Location: Norway
Posts: 16
Thanks for the response.
Reply With Quote
  #6 (permalink)  
Old 07-09-10, 10:17
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Well, of course he CAN do that, in a single result set. It's just going to require hackish coding.

It looks like he is creating a pseudo-XML output format, so he might be able to use some of the XML functionality to accomplish this.

WHY he would want to do this....well I'm probably in complete agreement with you on that, pootle.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #7 (permalink)  
Old 07-09-10, 10:29
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
What you are describing sounds like group supression (where you suppress repeating columns at the left side of a report). If that's what you're thinking, this is a presentaton issue so it should be handled at the client instead of at the server. Group supression is handled by nearly every reporting engine that I've ever seen, so doing it in code is almost always wasted effort.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
Reply

Tags
dilemma, query, sql, tables

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On