根据某些条件部分地转动表格?

时间:2017-01-18 14:38:40

标签: oracle

我无法弄清楚如何创建此视图,但这就是我现在所拥有的:

 ITEM     CODE       AGE      TYPE      INFO
------  --------  --------  --------  --------
1         A10        NEW       4        INFO A10           
2         B12        NEW       4        INFO B12
3         B15        NONE      4        INFO B15 
4         T11        NEW       4        INFO T11
4         T14        OLD       3        INFO T14 
4         T15        OLD       2        INFO T15
5         R01        OLD       4        INFO R01
5         R71        OLD       4        INFO R71
5         R55        OLD       3        INFO R55
5         R30        OLD       2        INFO R30

我想做的是:

  • 删除“ITEM”列上的重复值;
  • 根据以下内容将这些行删除成为列:
    • 如果itens中至少有一个AGE ='NEW'而不是将其保留为主列'CODE'
    • 如果没有AGE ='NEW',请将第一行作为主列'CODE',无论是哪一行都无关紧要。
  • AGE,TYPE和INFO列将基于主要栏目“CODE”的信息
  • 新列数CODES(CODE_2,CODE_3,...)固定为10
ITEM     CODE     CODE_2    CODE_3    CODE_4     AGE       TYPE     INFO
------  --------  --------  --------  --------  --------  -------- --------
1         A10         -          -          -      NEW       4      INFO A10
2         B12         -          -          -      NEW       4      INFO B12
3         B15         -          -          -      NEW       4      INFO B15
4         T11        T14        T15         -      NONE      4      INFO T11
5         R01        R71        R55        R30     OLD       4      INFO R01

有可能实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

正如你所说的关于视图的创建,我想你需要稳定的列集。在这种情况下,我使用类似的东西:

with s (item, code, age, type, info) as 
(select 1, 'A10', 'NEW', 4, 'INFO A10'  from dual union all
select 2, 'B12', 'NEW', 4, 'INFO B12'  from dual union all
select 3, 'B15', 'NONE', 4, 'INFO B15'  from dual union all
select 4, 'T11', 'NEW', 4, 'INFO T11'  from dual union all
select 4, 'T14', 'OLD', 3, 'INFO T14'  from dual union all
select 4, 'T15', 'OLD', 2, 'INFO T15'  from dual union all
select 5, 'R01', 'OLD', 4, 'INFO R01'  from dual union all
select 5, 'R71', 'OLD', 4, 'INFO R71 '  from dual union all
select 5, 'R55', 'OLD', 3, 'INFO R55'  from dual union all
select 5, 'R30', 'OLD', 2, 'INFO R30'  from dual 
)
select item,
       max(code)as CODE,
       max(code_2) as CODE_2,
       max(code_3) as CODE_3,
       max(code_4) as CODE_4,
       max(code_5) as CODE_5,
       max(code_6) as CODE_6,
       max(code_7) as CODE_7,
       max(code_8) as CODE_8,
       max(code_9) as CODE_9,
       max(code_10) as CODE_10,
       max(age)  as age,
       max(type)  as type,
       max(info)  as info
FROM (
select item, 
       decode(code_name,1,code) as code,
       decode(code_name,2,code) as code_2,
       decode(code_name,3,code) as code_3,
       decode(code_name,4,code) as code_4,
       decode(code_name,5,code) as code_5,
       decode(code_name,6,code) as code_6,
       decode(code_name,7,code) as code_7,
       decode(code_name,8,code) as code_8,
       decode(code_name,9,code) as code_9,
       decode(code_name,10,code) as code_10,
       decode(code_name,1,age) as age,
       decode(code_name,1,type) as type,
       decode(code_name,1,info) as info
from (select s.*,
       row_number() over (partition by item order by decode(age,'NEW',0,1),CODE) as code_name
  from s))
  group by item;