我无法弄清楚如何创建此视图,但这就是我现在所拥有的:
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 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
有可能实现这个目标吗?
答案 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;