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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Multiple rows into one query result row...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-24-05, 14:55
EOS3 EOS3 is offline
Registered User
 
Join Date: Feb 2003
Posts: 5
Multiple rows into one query result row...

I have an abstract relational database underneath some business objects... For instance, say I have two tables...

TABLE 1: A simple list of people...

ID USER
---------
1 Mike
2 John


TABLE 2: Name/Value pairs of attributes linked to table 1 by ID...

ID NAME VALUE
-------------------
1 Hair Brown
1 Eyes Blue
2 Weight 200

So you can see from this that Mike has brown hair and blue eyes, and that John weighs 200 lbs.

I want a query that selects a person and all their attributes (whatever they may be), and returns the results on one row like this (when run with a WHERE clause that selects user Mike).

USER HAIR EYES
-------------------
Mike Brown Blue


And returns this when run with a WHERE clause that selects user John...

USER WEIGHT
---------------
John 200

Any ideas? Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 01-24-05, 15:10
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Wow! Talk about stuff that will give you nightmares!

-PatP
Reply With Quote
  #3 (permalink)  
Old 01-24-05, 17:06
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Thumbs down

This question has been asked and anwered hundreds of times in all forums. Do some research!
Hint: Search for PIVOT TABLE, CROSSTAB QUERY, etc...
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #4 (permalink)  
Old 01-24-05, 17:34
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by LKBrwn_DBA
This question has been asked and anwered hundreds of times in all forums. Do some research!
Hint: Search for PIVOT TABLE, CROSSTAB QUERY, etc...
I think this one is a twist on the usual cross tab... It looks like they want the query schema to change from row to row, which goes way beyond a cross-tab in my mind. It makes my head hurt just thinking about it. A cross-tab seems simple to code and use in comparison to this request.

-PatP
Reply With Quote
  #5 (permalink)  
Old 01-24-05, 17:57
EOS3 EOS3 is offline
Registered User
 
Join Date: Feb 2003
Posts: 5
Yep...

Thanks, Pat. I'm being bludgeoned on a few boards over this. People keep saying cross-tab, which is not what I'm after here...

It does indeed make the head hurt...
Reply With Quote
  #6 (permalink)  
Old 01-24-05, 18:01
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
I would classify this as a candidate for a recursive query. Search through this forum for some good ideas.
Reply With Quote
  #7 (permalink)  
Old 01-24-05, 18:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
candidate for dynamic sql

step 1: select distinct name from table2 where id=n

if you don't know n, do a join and use WHERE table1.name = 'fred'

step 2: construct multiple LEFT OUTER JOIN query, from table1 to table2 as many times as there are attributes that fred has (from step1), aliasing each table2.value to the corresponding column name

step 3: execute the dynamic query

easy peasy
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 01-24-05, 18:43
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I think that Rudy's suggestion is a good one, if you can allow all of the rows in a given result set to have the same schema. This is probably the closest answer possible to what you want using standard SQL tools. Supporting irregularly shaped result sets is possible using some tools, but not using standard recordset-oriented tools.

As Fibber used to say: "T'ain't pretty, McGee!"

-PatP
Reply With Quote
Reply

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