transpose rows

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
Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DBACommented:
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
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
``````
0

Experts Exchange Solution brought to you by