【问题标题】:Convert columns to rows in oracle [duplicate]在oracle中将列转换为行[重复]
【发布时间】:2014-01-27 13:34:02
【问题描述】:

这是我在 oracle 11g 中的表:

**date          qty1            qty2             qty3           qty4**
2-Feb-14            61           64             52           54
2-Mar-14           124          130            149          156

我想把它转换成下表。即在日期上加上 7 天并转置数量。我有 qty52 这样的指标

***date        qty***
**2-Feb-14        61**
9-Feb-14          64
16-Feb-14         52
23-Feb-14         54
**2-Mar-14        124**
9-Mar-14          130
16-Mar-14         149
23-Mar-14         156

【问题讨论】:

    标签: oracle oracle11g oracle10g oracle-sqldeveloper


    【解决方案1】:

    试一试:

    WITH t(my_date, val, val2, val3, val4)
    AS ( 
    SELECT to_date('01/01/2014 12:00:00 AM', 'dd/mm/yyyy hh:mi:ss am'), 1,2,3,4 from dual
    UNION ALL
    SELECT to_date('01/02/2014 12:00:00 AM', 'dd/mm/yyyy hh:mi:ss am'), 5,6,7,8 FROM dual
    )
    SELECT (my_date-7) + (row_number() OVER (partition by my_date ORDER BY my_date)*7)  my_date, value as qty
    FROM (
         ( SELECT my_date, val, val2, val3, val4 FROM t
         ) unpivot ( value FOR value_type IN (val, val2, val3, val4) ) ); 
    


    输出:

    MY_DATE                        QTY
    ----------------------- ----------
    01/01/2014 12:00:00 AM           1 
    08/01/2014 12:00:00 AM           2 
    15/01/2014 12:00:00 AM           3 
    22/01/2014 12:00:00 AM           4 
    01/02/2014 12:00:00 AM           5 
    08/02/2014 12:00:00 AM           6 
    15/02/2014 12:00:00 AM           7 
    22/02/2014 12:00:00 AM           8 
    

    【讨论】:

      【解决方案2】:
      select date,qty from
      (select date,qty1 as qty
      from tbl
      union
      select date+7 as date,qty2 as qty
      from tbl
      union
      select date+14 as date,qty3 as qty
      from tbl
      union
      select date+21 as date,qty4 as qty
      from tbl)
      order by date
      

      【讨论】:

      • 谢谢你们!!所有的建议都有效!!
      【解决方案3】:

      如果您有 Oracle 11g,我会考虑使用 UNPIVOT 来实现。

      select
        start_date + to_number(week_number) * 7,
      qty
      from (
        select *
        from quantity_data 
        unpivot (qty for week_number 
                 in (qty1 as '0', qty2 as '1', qty3 as '2', qty4 as '3')) 
      )
      

      这是 ajmalmhd04 示例的替代方法,使用 to_number 代替 row_number 分析函数。不过,ajmalmhd04 的答案可能更通用

      如果您还没有 Oracle 11g,那么试试这个选项:

      with pivot_data as (
        select 0 as pivot_col from dual union all
        select 1 from dual union all
        select 2 from dual union all
        select 3 from dual
      )
      select 
        start_date + (7 * pivot_col) as start_date,
        case 
          when pivot_col = 0 then qty1
          when pivot_col = 1 then qty2
          when pivot_col = 2 then qty3
          when pivot_col = 3 then qty4 end as qty
      from 
        quantity_data cross join pivot_data
      order by 1
      

      【讨论】:

        【解决方案4】:

        试试这个

        with tab(date_d,qty1,qty2,qty3,qty4) as (
           select '2-Feb-14',61,64,52,54 from dual union all
           select '2-Mar-14',124,130,149,156 from dual),
        tab2(dd, ss) as (select date_d, qty1||','||qty2||','||qty3||','||qty4  from tab)
        select to_date(dd) + ((level-1) * 7) "DATE", regexp_substr(ss, '[^(,)]+', 1, level) "QTY"
          from tab2
        connect by level <= length(ss) - length(replace(ss, ',')) + 1
          and prior ss = ss
          and prior sys_guid() is not null
        

        输出

        |                            DATE | QTY |
        |---------------------------------|-----|
        |    March, 02 2014 00:00:00+0000 | 124 |
        |    March, 09 2014 00:00:00+0000 | 130 |
        |    March, 16 2014 00:00:00+0000 | 149 |
        |    March, 23 2014 00:00:00+0000 | 156 |
        | February, 02 2014 00:00:00+0000 |  61 |
        | February, 09 2014 00:00:00+0000 |  64 |
        | February, 16 2014 00:00:00+0000 |  52 |
        | February, 23 2014 00:00:00+0000 |  54 |
        

        如果它符合您的要求,请告诉我。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-14
          • 2020-02-23
          • 2018-11-11
          • 2019-09-13
          • 2021-03-02
          • 2021-08-07
          • 2013-06-08
          • 1970-01-01
          相关资源
          最近更新 更多