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 > DB2 > SQL: converting column data to one row

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-19-03, 01:15
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
SQL: converting column data to one row - invert - pivot

Hi,

I have table JOBS. I would like to write SQL to get all column data into one row separated with commas.

Sample:

Source data:
JOBID
-----
1
2
3
4
5
...
n


Result:
ALL_JOBS
--------
1,2,3,4,5,...,n

How to write such SELECT clause?

Thanks,
Grofaty

Last edited by grofaty; 12-19-03 at 03:02.
Reply With Quote
  #2 (permalink)  
Old 12-19-03, 05:17
nitingm nitingm is offline
Registered User
 
Join Date: Jul 2003
Location: Austin, TX, USA
Posts: 278
I assume ID is primary key

HI Grofaty.

Can you please try this

db2 select a.job_id concat ',' concat z.job_id as all_jobs from (select job_id from t4 fetch first 1 row only) as a, (select job_id from (select job_id from t4 fetch first 2 rows only) as a except select * from (select job_id from t4 fetch first 1 row only) as b) as z

HTH

Nitin.

OOPS I FORGOT THIS WORKS FOR 2 ROWS U NEED TO MODIFY THE SAME FOR MANY ROWS.

TABLE

JOB_ID

------

1

2

2 record(s) selected.

OUTPUT

1

---

1,2
__________________
HTH

Nitin

Ask the experienced rather than the learned

Last edited by nitingm; 12-19-03 at 05:20.
Reply With Quote
  #3 (permalink)  
Old 12-19-03, 06:01
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,

Sorry this does not work. I just forgot to mention that I have db2 version 7.2 for Windows 2000. In DB2 v7.2 does not work the "fetch first n rows only" in subselect. This is a new feature in DB2 v8.

Any other idea?

Thanks,
Grofaty
Reply With Quote
  #4 (permalink)  
Old 12-19-03, 08:44
jsander jsander is offline
Registered User
 
Join Date: Apr 2003
Posts: 191
Hi Grofaty,

this won't work in general unless you consider common table expressions, which probably are no standard SQL, though.

Johann

Quote:
Originally posted by grofaty
Hi,

Sorry this does not work. I just forgot to mention that I have db2 version 7.2 for Windows 2000. In DB2 v7.2 does not work the "fetch first n rows only" in subselect. This is a new feature in DB2 v8.

Any other idea?

Thanks,
Grofaty
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