日期:2014-05-17 浏览次数:20675 次
declare @A table(配货单号 int, 序号 int, 商品条形码 varchar(5), 订货数 int, 送货数 int) insert into @A select 9300876, 1, 'A', 123, NULL union all select 9300876, 2, 'B', 10, NULL union all select 9300876, 3, 'C', 1, NULL union all select 9300876, 4, 'D', 50, NULL union all select 9300876, 5, 'E', 20, NULL union all select 9300877, 1, 'F', 3, NULL union all select 9300877, 2, 'JH', 3, NULL union all select 9300877, 3, 'JHA', 3, NULL union all select 9300878, 1, 'JHA', 3, NULL union all select 9300878, 2, 'JHC', 3, NULL declare @B table(配货单号 int, 序号 int, 送货数 int) insert into @B select 9300876, 5, 15 union all select 9300877, 1, 3 union all select 9300877, 2, 3 union all select 9300878, 1, 3 union all select 9300878, 2, 2 ;with cte as ( select a.配货单号 from (select 配货单号,count(1) cnt from @A group by 配货单号)a, (select 配货单号,count(1) cnt from @B group by 配货单号)b where a.配货单号=b.配货单号 and a.cnt=b.cnt ) update @A set 送货数=b.送货数 from @A a inner join @B b on a.配货单号=b.配货单号 and a.序号=b.序号 where a.配货单号 in (select 配货单号 from cte) select * from @A /* 配货单号 序号 商品条形码 订货数 送货数 ----------- ----------- ----- ----------- ----------- 9300876 1 A 123 NULL 9300876 2 B 10 NULL 9300876 3 C 1 NULL 9300876 4 D 50 NULL 9300876 5 E 20 NULL 9300877 1 F 3 NULL 9300877 2 JH 3 NULL 9300877 3 JHA 3 NULL 9300878 1 JHA 3 3 9300878 2 JHC 3 2 */
------解决方案--------------------
--> 测试数据:[ta] IF OBJECT_ID('[ta]') IS NOT NULL DROP TABLE [ta] GO CREATE TABLE [ta]([配货单号] INT,[序号] INT,[商品条形码] VARCHAR(3),[订货数] INT,[送货数] SQL_VARIANT) INSERT [ta] SELECT 9300876,1,'A',123,NULL UNION ALL SELECT 9300876,2,'B',10,NULL UNION ALL SELECT 9300876,3,'C',1,NULL UNION ALL SELECT 9300876,4,'D',50,NULL UNION ALL SELECT 9300876,5,'E',20,NULL UNION ALL SELECT 9300877,1,'F',3,NULL UNION ALL SELECT 9300877,2,'JH',3,NULL UNION ALL SELECT 9300877,3,'JHA',3,NULL UNION ALL SELECT 9300878,1,'JHA',3,NULL UNION ALL SELECT 9300878,2,'JHC',3,NULL GO --> 测试语句: --> 测试数据:[tb] IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb] GO CREATE TABLE [tb]([配货单号] INT,[序号] INT,[送货数] INT) INSERT [tb] SELECT 9300876,5,15 UNION ALL SELECT 9300877,1,3 UNION ALL SELECT 9300877,2,3 UNION ALL SELECT 9300878,1,3 UNION ALL SELECT 9300878,2,2 GO --> 测试语句: ;with cte as ( select [配货单号] from ta as t group by [配货单号] having count(*)=( select count(*) from ta as a,tb as b where a.[配货单号]=b.[配货单号] and a.[序号]=b.[序号] and a.[配货单号]=t.[配货单号] group by a.[配货单号] ) ) update