下面是一张仓库间的调拨单(只列出几个主要字段),行类型 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
---------------------------------------------
订单号 行类型 仓库 数量 商品代码
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
解决方案 »
- 简单问题
- 符合索引由两个字段组成,检索时按第二个字段为条件检索,索引能发挥作用吗?
- C#查询上个时间,判断DataSet是否为空,求教
- 创建视图索引出错,查询表加上(nolock) 就出错,高手请进...
- SQL 2000 CPU占用100%
- 这样的2个表应该怎么操作 60分全部献上了
- 同步更新远程数据库内的一张表内的部分内容
- 請問該sql語句執行的效率怎麼樣:SELECT TOP1 * FROM TABLE WHERE FIELDS='aa',謝謝
- 怎样用将一个文本文件导入到Access的数据表中。(用SQL语句实现)
- 如何实现sql server的复制
- 本游标和事务错在哪里?
- 【20080411】新旧数据库数据导入问题
行类型='F',
出货仓库 =max(case when 行类型='F' then 仓库 else null end),
行类型='T',
入库仓库 ==max(case when 行类型='T' then 仓库 else null end),
商品,
数量 = max( 数量)
from ta
group by 订单号,商品代码
行类型='F',
出货仓库 =max(case when 行类型='F' then 仓库 else null end),
行类型='T',
入库仓库 =max(case when 行类型='T' then 仓库 else null end),
商品,
数量 = max( 数量)
from ta
group by 订单号,商品代码
行类型 = 'F' ,
max(case 行类型 when 'F' then 仓库 end) 出货仓库,
行类型 = 'T' ,
max(case 行类型 when 'T' then 仓库 end) 入库仓库,
商品代码 商品,
max(case 行类型 when 'T' then 数量 end) 入库数量
from 调拨单
group by 订单号,商品代码
在PL/SQL 中 语句应该是 怎样的 啊
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
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 行)
*/
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
在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 订单号,商品代码
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'
另一种 拆分成两表,根据关联连接join
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.订单号