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 > How to combine two rows into one row with same ID?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-08-10, 02:44
mikkom mikkom is offline
Registered User
 
Join Date: Mar 2010
Posts: 4
How to combine two rows into one row with same ID?

I have a data that needs to be arranged from two rows into one row as follow:

From two rows into -->
ID X Text
1234 1 abc
1234 2 xyz

One row -->
ID Text1 Text2
1234 abc xyz

If field X=1, then Text needs be put into field Text1 and if X=2, then Text needs to be put into Text2.

So how I can do that? I think I need to use CASE function somehow?
Reply With Quote
  #2 (permalink)  
Old 07-08-10, 06:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, CASE
Code:
SELECT id
     , MAX(CASE WHEN x = 1 THEN text ELSE NULL END) AS text1
     , MAX(CASE WHEN x = 2 THEN text ELSE NULL END) AS text2
  FROM daTable
GROUP
    BY id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-08-10, 07:12
mikkom mikkom is offline
Registered User
 
Join Date: Mar 2010
Posts: 4
It works now. Thanks!
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