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 > How to make a group

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-07-06, 14:50
Mastakey Mastakey is offline
Registered User
 
Join Date: Sep 2006
Posts: 10
How to make a group

Hello, I am new here and I am novice at DB2 and I am having some diffultity with a simple problem.

I have a table like this:
id word
1 abc
1 def
1 ghi
2 one
2 two
2 three
3 yes
3 no
3 maybe


I want to make a view like this using the table above:

id words
1 abc, def, ghi
2 one, two, three
3 yes, no, maybe


Don't neccessarily need the commas there, spaces or any other separator is fine.
I am not sure how I can go about doing this purely in SQL. Is it possible to do this without using cursors or loops? i.e. is there a easy and simple solution to this? Any sugguestions or Help is well appreciated.


Thanks!

Edit: I am running DB2 8.2.1

Last edited by Mastakey; 09-07-06 at 16:06.
Reply With Quote
  #2 (permalink)  
Old 09-08-06, 03:12
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
you can get a better idea from following thread
SQL query

--Rahul Singh
Reply With Quote
  #3 (permalink)  
Old 09-08-06, 05:40
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
ID 2
----------- --------------------------------------------------------------
1 abc,ghi,def
2 maybe,yes,no
Reply With Quote
  #4 (permalink)  
Old 09-08-06, 05:41
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
Assuming name of table as test and considering that for 1 id there are only three or less distinct nos.

with temp (id,word) as
(select id, min(word)||','||max(word) con from test group by id)
select a.id,a.word||','||b.word from temp A , test B where a.id=b.id
and locate(b.word,a.word)=0

ID 2
----------- --------------------------------------------------------------
1 abc,ghi,def
2 maybe,yes,no

--Rahul Singh
Reply With Quote
  #5 (permalink)  
Old 09-08-06, 15:12
Mastakey Mastakey is offline
Registered User
 
Join Date: Sep 2006
Posts: 10
Hey, thanks for the quick reply!
The suggestion on other thread works as usual but I am more interested in the query in your last post. It works as well, but my DB has much more words than 3. Is there a cheap way to make your query work for more words than 3? I expect each id to have about 20 words!

Thanks again!


Thanks again!
Reply With Quote
  #6 (permalink)  
Old 09-09-06, 06:12
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
The CTE ("with" expression) allows for recursion: within the first select, which is the definition of table "temp", that table "temp" may be used.

I have the feeling that recursion could give the general solution: recursively concatenating fields with the same id but from different rows, until no rows are left in that group. But I can't come up with a working query yet ...

Somebody else who sees the light?
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #7 (permalink)  
Old 09-13-06, 00:00
arlf arlf is offline
Registered User
 
Join Date: May 2006
Posts: 16
If you can add a sequence column (like an identity column), the next query could be a solution.

WITH TEMP1 (ID1, WORD1, SEQ1) AS
(SELECT ID, VARCHAR(RTRIM(MIN(WORD)),20), MIN(SEQ)
FROM TBL01 T
GROUP BY ID, SEQ, WORD
UNION ALL
SELECT A.ID, RTRIM(B.WORD1) || RTRIM(A.WORD), A.SEQ
FROM TEMP1 B, TBL01 A
WHERE A.ID = B.ID1
AND A.SEQ = B.SEQ1 + 1)
SELECT ID1, MAX(WORD1)
FROM TEMP1 C
GROUP BY ID1;

Saludos, ARLF.
Reply With Quote
  #8 (permalink)  
Old 09-13-06, 02:25
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
This will only work if the sequence number (1) has no holes, and (2) does not jump back and forth between groups of identical id.
But you can virtually add such a sequence number by using the OLAP function ROWNUMBER() OVER (ORDER BY id)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 09-13-06 at 02:29.
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