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 > Database table design

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-13-02, 12:49
sandrak sandrak is offline
Registered User
 
Join Date: Feb 2002
Posts: 1
Database table design

Hi,

I have a table in DB2 UDB with data that looks like this:
col1 col2 col3
1 |500 |15
1 |1500 |12
1 |1000 |10
2 |300 |12
2 |800 |10


I want to create a new table in DB2 UDB that takes this data and displays it as follows:
col1 col2 col3
1 |500,1500,1000 |15,12,10
2 |300,800 |12,10

Does anyone know how I can do this in DB2 UDB?

Last edited by sandrak; 02-13-02 at 17:56.
Reply With Quote
  #2 (permalink)  
Old 08-11-03, 16:31
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
Re: Database table design

As this is an old posting, this information is for those who have a similar question in the future.

One way to approach this problem is to create a function(s) which contains a FOR loop which would concatenate the data found for the value of col1 passed to the function. Example:
CREATE FUNCTION testf(id integer)
LANGUAGE SQL
NOT DETERMINISTIC
NO EXTERNAL ACTION
READS SQL DATA
RETURNS VARCHAR(1000)
BEGIN ATOMIC
FOR X AS
SELECT col2 FROM t
WHERE col1 = id
DO
SET v_value = col2 || ', ';
END FOR;
RETURN SUBSTR(v_value, 1, LENGTH(v_value) - 2);
END

To use it:
INSERT INTO test2 (col1, col2, col3)
SELECT col1, testf(col1), testf2(col1) FROM t


Quote:
Originally posted by sandrak
Hi,

I have a table in DB2 UDB with data that looks like this:
col1 col2 col3
1 |500 |15
1 |1500 |12
1 |1000 |10
2 |300 |12
2 |800 |10


I want to create a new table in DB2 UDB that takes this data and displays it as follows:
col1 col2 col3
1 |500,1500,1000 |15,12,10
2 |300,800 |12,10

Does anyone know how I can do this in DB2 UDB?
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