表1product   bomid  qnt type
a          1      1   压缩机
a          2       1   压缩机
a          3       2    电机
b          4       1     电机
b          5       2      压缩机
b          6       1      电机希望生成product   压缩机1编码  qnt 压缩机2编码 压缩机2数量   电机1编码  电机1数量  电机2编码  电机2数量
a           1           1    2           2             3         2          0          0
b           5           2    0            0            4          1         6          1   请各位高手指点一下,谢谢.           

解决方案 »

  1.   

    with tb as(
    select 'a'  product, 1  bomid ,1  qnt, '压缩机' type from dual union all
    select 'a' ,2 ,1 ,'压缩机' from dual union all
    select 'a' ,3 ,2 ,'电机' from dual union all
    select 'b' ,4 ,1 ,'电机' from dual union all
    select 'b' ,5 ,2 ,'压缩机' from dual union all
    select 'b' ,6 ,1 ,'电机' from dual )
    select product,
    sum(case when bomid =1  or bomid=5 then bomid else 0 end ) 压缩机1编码,
    sum(case when bomid =1  or bomid=5 then qnt else 0 end ) qnt ,
    sum(case when bomid =2  then bomid else 0 end ) 压缩机2编码,
    sum(case when bomid =2  then qnt else 0 end ) 压缩机2数量 ,
    sum(case when bomid =3 or bomid=4 then bomid else 0 end )电机1编码,
    sum(case when bomid =3 or bomid=4 then qnt else 0 end ) 电机1数量,
    sum(case when bomid =6 then bomid else 0 end ) 电机2编码,
    sum(case when bomid =6 then qnt else 0 end ) 电机2数量
    from tb 
    group by product
    只是针对数据,木有太大意义……