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 > display result in one row

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-07-04, 10:20
snoopyyu snoopyyu is offline
Registered User
 
Join Date: Jan 2004
Posts: 21
display result in one row

Hi,

I have two tables:

TableA has two columns: Type, ProductID
TableB has two columns: ProductID, ProductName

I need to get all the ProductName for a specific type and this is my SQL:
select TableB.ProductName
from TableA, TableB
where TableA.ProductID = TableB.ProductID
and TableA.Type = 2

The problem with the above SQL is that it returns one column with a few rows, like the following:

ProductA
ProductB
ProductC

Is there a way to have a single SQL that can return the results horizontal like:

ProductA, ProductB, ProductC

Thank you for help in advance.

Snoopy
Reply With Quote
  #2 (permalink)  
Old 05-07-04, 10:27
dbcat dbcat is offline
Registered User
 
Join Date: May 2002
Posts: 39
I don't think this is possible only through a single SQL.
Reply With Quote
  #3 (permalink)  
Old 05-07-04, 11:08
snoopyyu snoopyyu is offline
Registered User
 
Join Date: Jan 2004
Posts: 21
Re:

If not, what do you recommend to do? Thanks!
Reply With Quote
  #4 (permalink)  
Old 05-07-04, 11:24
dbcat dbcat is offline
Registered User
 
Join Date: May 2002
Posts: 39
Can you provide more info?
What DB, are you using a program to run this SQL??
Would it be possible for you load results into a data structure and format accordingly??
Reply With Quote
  #5 (permalink)  
Old 05-07-04, 11:29
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
This is called a cross-tab, and it really should be done on the client. They are much better suited to this kind of task.

There are a number of engine-specific ways to do cross-tabs. The problem is that they are engine specific, so one that works on Oracle won't work on DB2 or on SQL Server. We'll leave them out of this discussion for the moment.

There is a portable way to do it, as long as you have no "ties" (in your case that would be duplicate ProductName values) and a small, fixed maximum number of possible cross-tab values. That goes something like:
Code:
SELECT a.Type
,  Min(b1.ProductName)
+  Coalesce(', ' + Min(b2.ProductName), '') 
+  Coalesce(', ' + Min(b3.ProductName), '')
   FROM TableA AS a
   INNER JOIN TableB AS b1
      ON (b1.ProductID = a.ProductID)
   LEFT OUTER JOIN TableB AS b2
      ON (b2.productID = a.ProductID
      AND b1.ProductName < b2.ProductName)
   LEFT OUTER JOIN TableB AS b3
      ON (b3.productID = a.ProductID
      AND b2.ProductName < b3.ProductName)
   WHERE 2 = a.Type
   GROUP BY a.Type
You can expand this to get more than three values if needed.

-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