Oracle在枢轴内转动

时间:2015-12-08 19:57:20

标签: 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_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

1 个答案:

答案 0 :(得分:0)

正如您所说,您可以使用两年内的pivotself-join数据:

SQLFiddle

Pivot版本:

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