with
-- sample data
taba (jan, feb, mar, apr, tag) as
(select 'c' , null, null, null, 102 from dual union all
select null, 'd' , null, 't' , 100 from dual union all
select null, 'd' , null, null, 101 from dual union all
select 'c' , 'd' , 'b' , 't' , 103 from dual
),
tabb (name, tag) as
(select 'ally' , 100 from dual union all
select 'ben' , 101 from dual union all
select 'missy', 102 from dual union all
select 'noah' , 103 from dual
),
tabab as (
--select a.tag, b.name, nvl(a.jan,'na') as jan, nvl(a.feb,'na') as feb, nvl(a.mar,'na') as mar, nvl(a.apr,'na') as apr
select
a.tag, b.name,
decode(a.jan,'b','bird','c','chicken','d','dog','t','turtle',null) as jan,
decode(a.feb,'b','bird','c','chicken','d','dog','t','turtle',null) as feb,
decode(a.mar,'b','bird','c','chicken','d','dog','t','turtle',null) as mar,
decode(a.apr,'b','bird','c','chicken','d','dog','t','turtle',null) as apr
from taba a
join tabb b
on a.tag = b.tag
)
select
name,
listagg(val,',') WITHIN GROUP (ORDER by decode(mon, 'JAN',1,'FEB',2,'MAR',3,'APR',4,null)) as listval
from tabab
unpivot
(
val
for mon in (jan,feb,mar,apr)
)
group by name
order by 1
;