select * from emp14;
出力:
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL
100 Steven King SKING
次のように出力を列ごとに表示したい:
EMPLOYEE_ID 100
FIRST_NAME Steven
LAST_NAME King
EMAIL SKING
これを試して
SELECT col, val
FROM
(
SELECT to_char(EMPLOYEE_ID) AS EMPLOYEE_ID,FIRST_NAME,LAST_NAME, EMAIL FROM emp14
)t
UNPIVOT INCLUDE NULLS
(
VAL FOR col IN (EMPLOYEE_ID,FIRST_NAME,LAST_NAME, EMAIL)
);
別のアプローチ:
-- this type is going to contain value's name and the value itself
SQL> create type t_data is object(
2 col1 varchar2(31),
3 col2 varchar2(31)
4 );
5 /
Type created
SQL> create type t_table is table of t_data;
2 /
Type created
その後、次の方法でクエリを作成できます。
-- sample of data from the question + some extra data
-- just for the sake of demonstration.
SQL> with t1(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL) as
2 ( select 100, 'Steven', 'King', 'SKING' from dual union all
3 select 101, 'Steven1', 'King1', 'SKING1' from dual
4 )
5 select col1
6 , col2
7 from t1 t
8 cross join table(
9 t_table( t_data('Employee_id', t.employee_id)
10 , t_data('First_name', t.first_name)
11 , t_data('Last_Name' , t.last_name)
12 , t_data('e-mail' , t.email)
13 )
14 )
15 ;
結果:
COL1 COL2
------------------------------- -------------------------------
Employee_id 100
First_name Steven
Last_Name King
e-mail SKING
Employee_id 101
First_name Steven1
Last_Name King1
e-mail SKING1
8 rows selected
select concat('EMPLOYEE_ID ', EMPLOYEE_ID) as EMPLOYEE_ID,
concat( 'FIRST_NAME ' , FIRST_NAME) as FIRST_NAME,
concat('LAST_NAME ' , LAST_NAME) as LAST_NAME,
concat('EMAIL ' , EMAIL) as EMAIL
from email