需要附加透视列

时间:2014-09-16 07:42:39

标签: pivot

我的一个查询是 -

    select * from
    (
    select a.model_code,a.fiscal_quarter,a.scenario_no,a.gfcid,a.field_id,a.field_value
    from ccar_input_data a,ccar_model_feed_map b 
    where a.model_code = b.model_code (+) and a.field_id = b.field_id (+)
    and a.model_code = 'COMREPROJ'
    and a.scenario_no = 2 and a.fiscal_quarter = '2014-Q1' and a.gfcid = 112000000
    ) src
    pivot
    (
    max(field_value) for field_id in (10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
    31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61)
    )

给了我 a.model_code,a.fiscal_quarter,a.scenario_no,a.gfcid,a.field_id,a.field_value和10,11,... 61

我的第二个问题是----

        select * from
    (
    select d.field_id,d.field_value
    from ccar14a_output_view d,ccar_model_feed_map b 
    where d.model_code = b.model_code and d.field_id = b.field_id
    and d.model_code = 'COMREPROJ'
    and d.scenario_no = 2 and d.fiscal_quarter = '2014-Q1' and d.gfcid = 112000000
    ) src
    pivot
    (
    max(field_value) for field_id in (62,63,64,65,66,67,69)
    )

这给我列 62,63,... 69

我想将行设为 a.model_code,a.fiscal_quarter,a.scenario_no,a.gfcid,a.field_id,a.field_value and 10,11,... 61,62,63 ... 69

Pl帮助。我不知道如何才能加入这两个查询...需要帮助(我对sql很新)

1 个答案:

答案 0 :(得分:0)

试试这个:

select * from
(
select a.model_code,a.fiscal_quarter,a.scenario_no,a.gfcid,a.field_id,a.field_value
from ccar_input_data a,ccar_model_feed_map b 
where a.model_code = b.model_code (+) and a.field_id = b.field_id (+)
and a.model_code = 'COMREPROJ'
and a.scenario_no = 2 and a.fiscal_quarter = '2014-Q1' and a.gfcid = 112000000
UNION ALL
select null, null,null, null, d.field_id,d.field_value
from ccar14a_output_view d,ccar_model_feed_map b 
where d.model_code = b.model_code and d.field_id = b.field_id
and d.model_code = 'COMREPROJ'
and d.scenario_no = 2 and d.fiscal_quarter = '2014-Q1' and d.gfcid = 112000000
) src
pivot
(
max(field_value) for field_id in (10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61, 62,63,64,65,66,67,69)
)