oracle表如下,请用SQL语句显示成如下形式。
产品名   产品数量  仓库ID
A产品     1         1
A产品     2         3
B产品     1         1
C产品     6         4 
       …  …  …
用如下形式显示
产品名 仓库ID_1 仓库ID_2 仓库ID_3 仓库ID_4  
A产品  1        0        2         0
B产品  1        0        0         0
C产品  0        0        0         6

解决方案 »

  1.   


    --如果很多仓库就拼结字符串吧
    with t(产品名,产品数量,仓库ID) as(
    select 'A产品',1,1 from dual
    union all select 'A产品',2,3 from dual
    union all select 'B产品',1,1 from dual
    union all select 'C产品',6,4 from dual
    )
    select 产品名,
    sum(decode(仓库ID,1,产品数量,0)) 仓库ID_1,
    sum(decode(仓库ID,2,产品数量,0)) 仓库ID_2,
    sum(decode(仓库ID,3,产品数量,0)) 仓库ID_3,
    sum(decode(仓库ID,4,产品数量,0)) 仓库ID_4
    from t group by 产品名 order by 产品名;
    /*
    产品名           仓库ID_1   仓库ID_2   仓库ID_3   仓库ID_4                      
    -------------- ---------- ---------- ---------- ----------                      
    A产品                   1          0          2          0                      
    B产品                   1          0          0          0                      
    C产品                   0          0          0          6   
    */
      

  2.   

    select name ,max(case ckID when 1 then num else 0 end) '仓库1',
    max(case ckID when 2 then num else 0 end) '仓库2',
    max(case ckID when 3 then num else 0 end) '仓库3',
    max(case ckID when 4 then num else 0 end) '仓库4' from aaa
    group by name