| split result set into column (merged 6) [message #563814] |
Fri, 17 August 2012 05:53  |
 |
umeshchandraseth
Messages: 60 Registered: June 2011 Location: new delhi
|
Member |
|
|
Hi All,
i have a table emp with three column
columns are (empid varchar,empnomini varchar,nominitype varchar),
data in table like
empid empnomini nominitype
1 x B
1 y c
2 xx B
2 yyyy c
and i want data comes like
empid nominitype b nominitype c
1 x y
2 xx yyyy
please help me,
thanks in advance.
|
|
|
|
| Re: split result set into column [message #563822 is a reply to message #563814] |
Fri, 17 August 2012 06:06   |
Solomon Yakobson
Messages: 1442 Registered: January 2010
|
Senior Member |
|
|
SQL> with emp as (
2 select 1 empid,'x' empnomini,'B' nominitype from dual union all
3 select 1,'y','c' from dual union all
4 select 2,'xx','B' from dual union all
5 select 2,'yyyy','c' from dual
6 )
7 select empid,
8 max(case nominitype when 'B' then empnomini end) nominitype_b,
9 max(case nominitype when 'c' then empnomini end) nominitype_c
10 from emp
11 group by empid
12 /
EMPID NOMI NOMI
---------- ---- ----
1 x y
2 xx yyyy
SQL>
And if you are on 11g, use PIVOT.
SY.
|
|
|
|
|
|
|
|
|
|
|
|
| Re: split result set into column [message #563892 is a reply to message #563832] |
Fri, 17 August 2012 12:51  |
Solomon Yakobson
Messages: 1442 Registered: January 2010
|
Senior Member |
|
|
In SQL list of columns is static. So you must know upfront all possible values for nominitype:
select empid,
max(case nominitype when 'B' then empnomini end) nominitype_b,
max(case nominitype when 'c' then empnomini end) nominitype_c,
.
.
.
max(case nominitype when 'whatever' then empnomini end) nominitype_whatever
from emp
group by empid
/
SY.
|
|
|
|