Solved

transpose rows

Posted on 2012-04-02
2
359 Views
Last Modified: 2012-04-20
Hi,
I have table which looks like

table1
wc_id email
101    aa@yahoo.com
101    bb@yahoo.com
102    cc@yahoo.com
203    dd@yahoo.com

i want the results as
wc_id email
101    aa@yahoo.com;bb@yahoo.com
102    cc@yahoo.com
203    dd@yahoo.com

Thanks
0
Comment
Question by:sam2929
2 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Hi Sam,

This kind of question comes up quite a bit.  :)   I can be solved pretty easily with a function, stored procedure, recursive query, or sometimes just a simple join.

Quick question.  Is there a limit on how many email addresses that can be assigned to any wc_id value?  If it's one and there is a primary (or unique) key, a simple join is probably the easiest.  If the limit is more than 1, recursive SQL is worth trying, and if there can be "a lot", you'll need to determine if you want to continue lines or put "a lot" of email addresses on one line.


Kent
0
 
LVL 18

Accepted Solution

by:
daveslash earned 500 total points
Comment Utility
The following example should show you how to do it pretty simply. Adapt as you see fit.

HTH,
DaveSlash

select *
  from rec

SNUM         WORDNUM   WORD   
   1               1   This   
   1               2   is     
   1               3   a      
   1               4   fine   
   1               5   example
   2               1   Yet    
   2               2   another
   2               3   example
   2               4   blah1  
   2               5   blah2  
   2               6   blah3  

WITH rquery (snum, wordnum, sentence) as (
 SELECT base.snum, base.wordnum, base.word
    FROM rec base
   WHERE wordnum = 1

  UNION ALL

  SELECT t1.snum, t1.wordnum, sentence || ' ' || t1.word
    FROM rquery t0
    join rec t1
      on t0. snum = t1. snum
     AND t0.wordnum + 1 = t1.wordnum
)
select r1.SNUM,
       r1.SENTENCE
  from rquery r1             
 where r1.wordnum = (        
       select max(r2.wordnum)
       from rquery r2         
      where r2.snum = r1.snum)

SNUM   SENTENCE                             
   1   This is a fine example               
   2   Yet another example blah1 blah2 blah3

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now