1

Example Data Sql fiddle http://sqlfiddle.com/#!15/c8a17/4

My Query

select
    unnest(array[g,g1,g2]) as disp,
    unnest(array[g,g||'-'||g1,g||'-'||g1||'-'||g2]) as grp,
    unnest(array[1,2,3]) as ord,
    unnest(array['assesvalue','lst2','salesvalue','itemprofit','profitper','itemstockvalue'])as analysis,
    unnest(array[value1,tt,sv,tp,per,tsv])as val 
from ( 
    select 
        g,
        g1,
        g2,
        sum(value1) as value1,
        sum(tt) as tt,
        sum(sv) as sv,
        sum(tp) as tp,
        sum(per) as per,
        sum(tsv) as tsv 
    from table1 
    group by g,g1,g2
) as ta

It Show the output like this

disp    grp    ord    analysis             val
A       A       1       assesvalue          100
B       A-B     2       lst2                30
C       A-B-C   3       salesvalue           20
A       A       1       itemprofit           5
B       A-B     2       profitper            1
C       A-B-C   3      itemstockvalue        10

Expected Result :

disp    grp    ord      analysis            val
A       A       1       assesvalue          100
A       A       1       lst2                30
A       A       1       salesvalue           20
A       A       1       itemprofit           5
A       A       1       profitper            1
A       A       1       itemstockvalue       10
B      A-B      2       assesvalue          100
B      A-B      2       lst2                30
B      A-B      2       salesvalue           20
B      A-B      2       itemprofit           5
B      A-B      2       profitper            1
B      A-B      2       itemstockvalue       10
C      A-B-C    3       assesvalue          100
C      A-B-C    3       lst2                30
C      A-B-C    3       salesvalue           20
C      A-B-C    3       itemprofit           5
C      A-B-C    3       profitper            1
C      A-B-C    3      itemstockvalue        10

In Query i am using multiple unnest. first 3 unnest inside have 3 columns other have 6 columns that its show wrong output but if last 2 unnest have less then 6 its show my expected result. what am doing wrong in query??

i am using postgresql 9.3

4

0 回答 0