下面是一张仓库间的调拨单(只列出几个主要字段),行类型 F 表示出库、T 表示入库,一个订单有多个商品,一个商品分两行,F 行表示出库,数量用负的表示,T行用正数表示,
---------------------------------------------
订单号 行类型 仓库 数量 商品代码
001 F 南京库 -100 10101002
001 T 苏州库 100 10101002
001 F 南京库 -251 10702001
001 T 苏州库 251 10702001
001 F 南京库 -360 10202006
001 T 苏州库 360 10202006
001 F 南京库 -560 10401052
001 T 苏州库 560 10401052
002 F 无锡库 -560 10401052
002 T 苏州库 560 10401052
002 F 无锡库 -360 10402151
002 T 苏州库 360 10402151
002 F 无锡库 -760 10403001
002 T 苏州库 760 10403001
---------------------------------------
我的目的是把F和T行拆分出来,出库仓库、入库仓库、数量拆分成新的列,如下:
---------------------------------------
订单号 行类型 出货仓库 行类型 入库仓库  商品          数量
001 F 南京库 T 苏州库 10101002 100
001 F 南京库 T 苏州库 10702001 251
001 F 南京库 T 苏州库 10202006 360
001 F 南京库 T 苏州库 10401052 560
002 F 无锡库 T 苏州库 10401052 560
002 F 无锡库 T 苏州库 10402151 360
002 F 无锡库 T 苏州库 10403001 760

解决方案 »

  1.   

    订单号 行类型 仓库 数量 商品代码 select 订单号,
           行类型='F',
           出货仓库 =max(case when 行类型='F' then 仓库 else null end),
           行类型='T',
           入库仓库 ==max(case when 行类型='T' then 仓库 else null end),
           商品,
            数量 = max( 数量)
    from ta
    group by 订单号,商品代码
      

  2.   

    多个行等号select 订单号,
           行类型='F',
           出货仓库 =max(case when 行类型='F' then 仓库 else null end),
           行类型='T',
           入库仓库 =max(case when 行类型='T' then 仓库 else null end),
           商品,
            数量 = max( 数量)
    from ta
    group by 订单号,商品代码
      

  3.   

    select 订单号 , 
    行类型 = 'F' ,
    max(case 行类型 when 'F' then 仓库 end) 出货仓库,
    行类型 = 'T' ,
    max(case 行类型 when 'T' then 仓库 end) 入库仓库,
    商品代码 商品,
    max(case 行类型 when 'T' then 数量 end) 入库数量
    from 调拨单
    group by 订单号,商品代码
      

  4.   

    大哥!
    在PL/SQL 中 语句应该是 怎样的 啊 
      

  5.   

    create table tb(订单号 varchar(10), 行类型 varchar(10),仓库 varchar(10),数量 int ,商品代码 varchar(10))
    insert into tb select '001','F','南京库',-100,'10101002'
    insert into tb select '001','T','苏州库',100,'10101002'
    insert into tb select '001','F','南京库',-251,'10702001'
    insert into tb select '001','T','苏州库',251,'10702001'
    insert into tb select '001','F','南京库',-360,'10202006'
    insert into tb select '001','T','苏州库',360,'10202006'
    insert into tb select '001','F','南京库',-560,'10401052'
    insert into tb select '001','T','苏州库',560,'10401052'
    insert into tb select '002','F','无锡库',-560,'10401052'
    insert into tb select '002','T','苏州库',560,'10401052'
    insert into tb select '002','T','无锡库',-360,'10402151'
    insert into tb select '002','T','苏州库',360,'10402151'
    insert into tb select '002','T','无锡库',-760,'10403001'
    insert into tb select '002','T','苏州库',760,'10403001'select f.订单号,f.行类型,f.仓库 as 出货仓库,t.行类型,t.仓库 as 出货仓库,t.商品代码,t.数量 from (
    select * from tb where 行类型='f')f join (
    select * from tb where 行类型='t')t on f.订单号=t.订单号 and f.商品代码=t.商品代码
    001 F 南京库 T 苏州库 10101002 100
    001 F 南京库 T 苏州库 10702001 251
    001 F 南京库 T 苏州库 10202006 360
    001 F 南京库 T 苏州库 10401052 560
    002 F 无锡库 T 苏州库 10401052 560
      

  6.   

    create table tb(订单号 varchar(10) , 行类型 varchar(10) , 仓库 varchar(10), 数量 int,商品代码  varchar(10))
    insert into tb values('001' ,'F' ,'南京库' ,-100 ,'10101002') 
    insert into tb values('001' ,'T' ,'苏州库' ,100  ,'10101002') 
    insert into tb values('001' ,'F' ,'南京库' ,-251 ,'10702001') 
    insert into tb values('001' ,'T' ,'苏州库' ,251  ,'10702001') 
    insert into tb values('001' ,'F' ,'南京库' ,-360 ,'10202006') 
    insert into tb values('001' ,'T' ,'苏州库' ,360  ,'10202006') 
    insert into tb values('001' ,'F' ,'南京库' ,-560 ,'10401052') 
    insert into tb values('001' ,'T' ,'苏州库' ,560  ,'10401052') 
    insert into tb values('002' ,'F' ,'无锡库' ,-560 ,'10401052') 
    insert into tb values('002' ,'T' ,'苏州库' ,560  ,'10401052') 
    insert into tb values('002' ,'F' ,'无锡库' ,-360 ,'10402151') 
    insert into tb values('002' ,'T' ,'苏州库' ,360  ,'10402151') 
    insert into tb values('002' ,'F' ,'无锡库' ,-760 ,'10403001') 
    insert into tb values('002' ,'T' ,'苏州库' ,760  ,'10403001') 
    goselect 订单号 , 
    行类型 = 'F' ,
    max(case 行类型 when 'F' then 仓库 end) 出货仓库,
    行类型 = 'T' ,
    max(case 行类型 when 'T' then 仓库 end) 入库仓库,
    商品代码 商品,
    max(case 行类型 when 'T' then 数量 end) 入库数量
    from tb
    group by 订单号,商品代码
    order by 订单号,商品代码drop table tb/*
    订单号        行类型  出货仓库       行类型  入库仓库       商品         入库数量        
    ---------- ---- ---------- ---- ---------- ---------- ----------- 
    001        F    南京库        T    苏州库        10101002   100
    001        F    南京库        T    苏州库        10202006   360
    001        F    南京库        T    苏州库        10401052   560
    001        F    南京库        T    苏州库        10702001   251
    002        F    无锡库        T    苏州库        10401052   560
    002        F    无锡库        T    苏州库        10402151   360
    002        F    无锡库        T    苏州库        10403001   760(所影响的行数为 7 行)
    */
      

  7.   

    create table tb(订单号 varchar(10), 行类型 varchar(10),仓库 varchar(10),数量 int ,商品代码 varchar(10))
    insert into tb select '001','F','南京库',-100,'10101002'
    insert into tb select '001','T','苏州库',100,'10101002'
    insert into tb select '001','F','南京库',-251,'10702001'
    insert into tb select '001','T','苏州库',251,'10702001'
    insert into tb select '001','F','南京库',-360,'10202006'
    insert into tb select '001','T','苏州库',360,'10202006'
    insert into tb select '001','F','南京库',-560,'10401052'
    insert into tb select '001','T','苏州库',560,'10401052'
    insert into tb select '002','F','无锡库',-560,'10401052'
    insert into tb select '002','T','苏州库',560,'10401052'
    insert into tb select '002','F','无锡库',-360,'10402151'
    insert into tb select '002','T','苏州库',360,'10402151'
    insert into tb select '002','F','无锡库',-760,'10403001'
    insert into tb select '002','T','苏州库',760,'10403001'select f.订单号,f.行类型,f.仓库 as 出货仓库,t.行类型,t.仓库 as 出货仓库,t.商品代码,t.数量 from (
    select * from tb where 行类型='f')f join (
    select * from tb where 行类型='t')t on f.订单号=t.订单号 and f.商品代码=t.商品代码
    001 F 南京库 T 苏州库 10101002 100
    001 F 南京库 T 苏州库 10702001 251
    001 F 南京库 T 苏州库 10202006 360
    001 F 南京库 T 苏州库 10401052 560
    002 F 无锡库 T 苏州库 10401052 560
    002 F 无锡库 T 苏州库 10402151 360
    002 F 无锡库 T 苏州库 10403001 760
      

  8.   


    在PL/SQL 中改为如下:select 订单号 , 
           'F' 行类型,
           max(case 行类型 when 'F' then 仓库 end) 出货仓库,
           'T' 行类型,
           max(case 行类型 when 'T' then 仓库 end) 入库仓库,
           商品代码 商品,
           max(case 行类型 when 'T' then 数量 end) 入库数量
    from tb
    group by 订单号,商品代码
    order by 订单号,商品代码
      

  9.   

    create table tb(订单号 varchar(10), 行类型 varchar(10),仓库 varchar(10),数量 int ,商品代码 varchar(10))
    insert into tb select '001','F','南京库',-100,'10101002'
    insert into tb select '001','T','苏州库',100,'10101002'
    insert into tb select '001','F','南京库',-251,'10702001'
    insert into tb select '001','T','苏州库',251,'10702001'
    insert into tb select '001','F','南京库',-360,'10202006'
    insert into tb select '001','T','苏州库',360,'10202006'
    insert into tb select '001','F','南京库',-560,'10401052'
    insert into tb select '001','T','苏州库',560,'10401052'
    insert into tb select '002','F','无锡库',-560,'10401052'
    insert into tb select '002','T','苏州库',560,'10401052'
    insert into tb select '002','F','无锡库',-360,'10402151'
    insert into tb select '002','T','苏州库',360,'10402151'
    insert into tb select '002','F','无锡库',-760,'10403001'
    insert into tb select '002','T','苏州库',760,'10403001'
    select 
       f.订单号,
       f.行类型,
       f.仓库 as 出货仓库,
       t.行类型,
       t.仓库 as 出货仓库,
       t.商品代码,t.数量 
    from tb f 
    join tb t on f.订单号=t.订单号 and f.商品代码=t.商品代码 and f.行类型='f' and t.行类型='t'
      

  10.   

    两种方法,一种  行转列
    另一种   拆分成两表,根据关联连接join
      

  11.   


    select a.订单号,a.行类型,a.仓库 as 出货仓库,b.行类型,b.仓库 as 入库仓库,a.商品代码,b.数量
    from tb a join tb b
    on a.商品代码=b.商品代码
    where a.订单号 = b.订单号
    and a.仓库 != b.仓库
    and a.行类型='F'
    and b.行类型='T'
    order by a.订单号