Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FIELD from row to COLUMN

Status
Not open for further replies.

fayevalentine

Programmer
Jun 2, 2004
38
MX
Hello

I have my select statement returns something like this:

id episode name
1 1 john
2 1 mary
3 1 null

is it posible get a result like this?

episode
1 john mary null

I have tried with pivot but with I need an aggregate function which I do not use,

can anyone guide me!!

Thanks in advance
 
HI

I find a posible solution

I find this instruction

select id_episode, listagg(nvl(name,'noname'), '|' ) within group (order by name) as names
from opinion where id_episode = 1
group by id_episode order by id_episode

this returns:

id_episode names
1 john|mary|noname

This workaround serves me for a while, hope this help you too, or if you can make field "names" to three columns I would appreciate you share.



Regards.
 
If you have a way to rank the records you want to put into columns, do this is a subquery, then feed this information to an outer query where you do the group by.
General example.

select
id_espisode,
max(case when rnk = 1 then name else null end) as 1stname,
max(case when rnk = 2 then name else null end) as 2stname,
max(case when rnk = 3 then name else null end) as 3rdname
From
(
select
id_espisode,
name,
rank() over(partition by id_espisode order by whatwillrankrecords) as rnk
from opinion
)
group by
id_espisode
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top