34,575
社区成员
发帖
与我相关
我的任务
分享
--========+++++++++++++++++++++++++++++++++++==========
--======= 每天都在进步,却依然追不上地球的自传=========
--======= By: zc_0101 At:2010-04-19 15:33:39=========
--========++++++++++++++++++++++++++++++++++++=========
--> 测试数据: #a
if object_id('tempdb.dbo.#a') is not null drop table #a
create table #a (销售订单号 varchar(4),销售序号 varchar(2),发运单号 varchar(4),发运单序号 varchar(2))
insert into #a
select 'D001','01','F001','01' union all
select 'D001','01','F002','01' union all
select 'D001','01','F003','01' union all
select 'D002','02','F001','02' union all
select 'D003','01','F002','02' union all
select 'D004','01','F002','03'
--> 测试数据: #b
if object_id('tempdb.dbo.#b') is not null drop table #b
create table #b (箱号 varchar(4),发运单号 varchar(4),发运单序号 varchar(2),销售订单号 varchar(4),销售序号 varchar(2),净重 numeric(4,2))
insert into #b
select 'X001','F001','01','D001','01',18.56 union all
select 'X002','F001','01','D001','01',13.56 union all
select 'X003','F002','01','D001','01',28.56 union all
select 'X004','F002','03','D001','01',21.56 union all
select 'X100','F001','01','D100','01',38.56 union all
select 'X101','F001','01','D100','01',48.56 union all
select 'X102','F001','01','D101','01',23.56 union all
select 'X103','F100','01','D001','01',11.56 union all
select 'X104','F101','02','D001','01',19.56
----------------查询------------
declare @d_d_h varchar(50),@d_d_x_h varchar(50)
select @d_d_h='D001',@d_d_x_h='01'
;with zc as(
select a.*,b.箱号,b.净重,b.销售订单号 调用销售订单号,b.销售序号 调用销售序号 from #a a
inner join #b b on a.发运单号=b.发运单号
and a.发运单序号=b.发运单序号
where a.发运单号 in(select 发运单号 from #a where 销售订单号=@d_d_h and 销售序号=@d_d_x_h)
and a.发运单序号 in(select 发运单序号 from #a where 销售订单号=@d_d_h and 销售序号=@d_d_x_h)
)
select 销售订单号,销售序号,发运单号,发运单序号,调用销售订单号,调用销售序号,sum(净重) 总重
from zc group by 销售订单号,销售序号,发运单号,发运单序号,调用销售订单号,调用销售序号
----------------结果--------------
/*
销售订单号 销售序号 发运单号 发运单序号 调用销售订单号 调用销售序号 总重
D001 01 F001 01 D001 01 32.12
D001 01 F001 01 D100 01 87.12
D001 01 F001 01 D101 01 23.56
D001 01 F002 01 D001 01 28.56
*/
--------------------SQL Server数据格式化工具-------------------
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------
use test
go
if object_id('test.dbo.tb1') is not null drop table tb1
-- 创建数据表
create table tb1
(
订单号 char(5),
订单序号 char(3),
发运单号 char(5),
发运单序号 char(3)
)
go
--插入测试数据
insert into tb1 select 'D001','01','F001','01'
union all select 'D001','01','F002','01'
union all select 'D001','01','F003','01'
union all select 'D002','02','F001','02'
union all select 'D003','01','F002','02'
union all select 'D004','01','F002','03'
go
if object_id('test.dbo.tb2') is not null drop table tb2
-- 创建数据表
create table tb2
(
箱号 char(5),
发运单号 char(5),
发运单序号 char(3),
销售订单号 char(5),
销售序号 char(3),
净重 float
)
go
--插入测试数据
insert into tb2 select 'X001','F001','01','D001','01',18.56
union all select 'X002','F001','01','D001','01',13.56
union all select 'X003','F002','01','D001','01',28.56
union all select 'X004','F002','03','D001','01',21.56
union all select 'X100','F001','01','D100','01',38.56
union all select 'X101','F001','01','D100','01',48.56
union all select 'X102','F001','01','D101','01',23.56
union all select 'X103','F100','01','D001','01',11.56
union all select 'X104','F101','02','D001','01',19.56
go
--代码实现
select tb2.销售订单号,tb2.销售序号,tb1.发运单号,tb1.发运单序号,tb2.销售订单号 调用销售订单号,tb2.销售序号 调用销售序号,sum(净重)发货数量
from tb1,tb2
where tb1.发运单号=tb2.发运单号 and tb1.发运单序号=tb2.发运单序号 and tb1.订单号 = 'D001' and tb1.订单序号 = '01'
group by tb2.销售订单号,tb2.销售序号,tb1.发运单号,tb1.发运单序号,tb2.销售订单号,tb2.销售序号
/*测试结果
销售订单号 销售序号 发运单号 发运单序号 调用销售订单号 调用销售序号 发货数量
----------------------------------------------------------------------------------------------
D001 01 F001 01 D001 01 32.12
D001 01 F002 01 D001 01 28.56
D100 01 F001 01 D100 01 87.12
D101 01 F001 01 D101 01 23.56
(4 行受影响)
*/
--select tb1.订单号,tb1.订单序号,tb1.发运单号,sum(tb2.净重)发货汇总 from tb1, tb2
--where tb1.发运单号 = tb2.发运单号 and tb1.发运单序号 = tb2.发运单序号 and tb1.订单号 = 'D001' and tb1.订单序号 = '01' and tb2.订单号 not in(
--select 订单号 from tb1 where 订单号<>'D001' and 发运单号 in (
--select 发运单号 from tb1 where 订单号 = 'D001' and 订单序号 = '01'))
--group by tb1.订单号,tb1.订单序号,tb1.发运单号
--D001 01 F001 01 D001 01 18.56+13.56 (本订单发本本订单的货)
--D001 01 F002 01 D001 01 28.56 (本订单发本本订单的货)
--D001 01 F001 01 D100 01 38.56+48.56 (本订单在发货时,调用了其他订单的货物)
--D001 01 F001 01 D101 01 23.56 (本订单在发货时,调用了其他订单的货物)
--d001-01分成几张发运单发货,而每张发运单可能发本订单的货物,同时可能调用了其他订单的货物
--(同样在明细中还可能存在D001-01这张订单被其他订单调货,这部分就必须去除),所以想得到本
--订单对应的发运单各自调用了那些货物(去除本订单被其他订单调用的货物数量)