unpivot中的Oracle pivot
我使用unpivot中的Oracle pivot,oracle,pivot,unpivot,Oracle,Pivot,Unpivot,我使用UNPIVOT创建了以下查询。它将列显示为行,这正是我想要的。现在,我必须在2012栏的旁边加上一个2013年的栏。这将是从mytable中选择的第二行 我不知道如何做到这一点…无论是一个支点或某种连接 这是一个压缩版本,因为mytable有80列 我的问题是: select value_type as " ", value as "2012" from ((select to_char(id_internal) as "ID_INTERNAL", to
UNPIVOT
创建了以下查询。它将列显示为行,这正是我想要的。现在,我必须在2012栏的旁边加上一个2013年的栏。这将是从mytable
中选择的第二行
我不知道如何做到这一点…无论是一个支点或某种连接
这是一个压缩版本,因为mytable有80列
我的问题是:
select value_type as " ", value as "2012"
from ((select to_char(id_internal) as "ID_INTERNAL",
to_char(tyear) as "YEAR",
to_char(transaction_id) as "TRANSACTION_ID",
to_char(in_date) as "IN_DATE",
to_char(name) as "NAME",
to_char(sid) as "SID",
to_char(address) as "ADDRESS",
to_char(city_state_zip) as "CITY_STATE_ZIP"
from mytable
where sid = 123456789
and tyear = 2012)
unpivot(value for value_type in (id_internal,
tyear,
transaction_id,
in_date,
name,
sid,
address,
city_state_zip)))
期望输出:
2012 2013
ID_INTERNAL 914008821991 914008821991
TYEAR 2012 2013
TRANSACTION_ID 6402962001234 6402962001234
IN_DATE 24-JAN-14 18-JAN-15
NAME BARBARA SMITH BARBARA SMITH
SID 123456789 123456789
ADDRESS 123 Main Street 777 BIGSBY ST
CITY_STATE_ZIP GREENSBORO, NC 12345-1234 CHARLESTON, SC 12345-1234
正如您所说,您可以使用两年的
pivot
或self-join
数据:
透视版本:
with t as (
select * from (
(select to_char(id_internal) as "C01_ID_INTERNAL",
to_char(tyear) as "TYEAR",
to_char(transaction_id) as "C02_TRANSACTION_ID",
to_char(in_date, 'yyyy-mm-dd') as "C03_IN_DATE",
to_char(name) as "C04_NAME",
to_char(sid) as "C05_SID",
to_char(address) as "C06_ADDRESS",
to_char(city_state_zip) as "C07_CITY_STATE_ZIP"
from mytable
where sid = 123456789 and tyear in (2012, 2013))
unpivot (value for value_type
in (c01_id_internal, c02_transaction_id, c03_in_date, c04_name,
c05_sid, c06_address, c07_city_state_zip))))
select substr(value_type, 5) vt, yr_2012, yr_2013
from t pivot (max(value) for tyear in ('2012' yr_2012, '2013' yr_2013))
order by value_type
加入版本:
with t as (
select * from (
(select to_char(id_internal) as "C01_ID_INTERNAL",
to_char(tyear) as "TYEAR",
to_char(transaction_id) as "C02_TRANSACTION_ID",
to_char(in_date, 'yyyy-mm-dd') as "C03_IN_DATE",
to_char(name) as "C04_NAME",
to_char(sid) as "C05_SID",
to_char(address) as "C06_ADDRESS",
to_char(city_state_zip) as "C07_CITY_STATE_ZIP"
from mytable
where sid = 123456789 and tyear in (2012, 2013))
unpivot (value for value_type
in (c01_id_internal, c02_transaction_id, c03_in_date, c04_name,
c05_sid, c06_address, c07_city_state_zip))))
select substr(value_type, 5) vt, t1.value v_2012, t2.value v_2013
from t t1 join (select * from t where tyear=2013) t2 using (value_type)
where t1.tyear = 2012
正如您所说,您可以使用两年的
pivot
或self-join
数据:
透视版本:
with t as (
select * from (
(select to_char(id_internal) as "C01_ID_INTERNAL",
to_char(tyear) as "TYEAR",
to_char(transaction_id) as "C02_TRANSACTION_ID",
to_char(in_date, 'yyyy-mm-dd') as "C03_IN_DATE",
to_char(name) as "C04_NAME",
to_char(sid) as "C05_SID",
to_char(address) as "C06_ADDRESS",
to_char(city_state_zip) as "C07_CITY_STATE_ZIP"
from mytable
where sid = 123456789 and tyear in (2012, 2013))
unpivot (value for value_type
in (c01_id_internal, c02_transaction_id, c03_in_date, c04_name,
c05_sid, c06_address, c07_city_state_zip))))
select substr(value_type, 5) vt, yr_2012, yr_2013
from t pivot (max(value) for tyear in ('2012' yr_2012, '2013' yr_2013))
order by value_type
加入版本:
with t as (
select * from (
(select to_char(id_internal) as "C01_ID_INTERNAL",
to_char(tyear) as "TYEAR",
to_char(transaction_id) as "C02_TRANSACTION_ID",
to_char(in_date, 'yyyy-mm-dd') as "C03_IN_DATE",
to_char(name) as "C04_NAME",
to_char(sid) as "C05_SID",
to_char(address) as "C06_ADDRESS",
to_char(city_state_zip) as "C07_CITY_STATE_ZIP"
from mytable
where sid = 123456789 and tyear in (2012, 2013))
unpivot (value for value_type
in (c01_id_internal, c02_transaction_id, c03_in_date, c04_name,
c05_sid, c06_address, c07_city_state_zip))))
select substr(value_type, 5) vt, t1.value v_2012, t2.value v_2013
from t t1 join (select * from t where tyear=2013) t2 using (value_type)
where t1.tyear = 2012