日期:2014-05-18 浏览次数:20752 次
use Tempdb go --> --> if not object_id(N'Tempdb..#T1') is null drop table #T1 Go Create table #T1([订单编号] nvarchar(21),[款号] int,[颜色] nvarchar(2),[尺码] nvarchar(2),[数量] decimal(18,8)) Insert #T1 select N'JHDD-2011-12-02-00002',699,N'米色',N'L',1.00000000 union all select N'JHDD-2011-12-02-00002',699,N'米色',N'M',2.00000000 union all select N'JHDD-2011-12-02-00002',699,N'米色',N'XL',15.00000000 Go if not object_id(N'Tempdb..#T2') is null drop table #T2 Go Create table #T2([款号] int,[颜色] nvarchar(2),[尺码] nvarchar(3)) Insert #T2 select 699,N'米色',N'L' union all select 699,N'米色',N'M' union all select 699,N'米色',N'XL' union all select 699,N'米色',N'XXL' Go SELECT a.*, b.[数量] FROM (Select b.*,a.* from #T2 AS a,(SELECT DISTINCT [订单编号] FROM #T1) b) AS a LEFT JOIN #T1 AS b ON a.[订单编号]=b.[订单编号] AND a.[款号]=b.[款号] AND a.[颜色]=b.[颜色] AND a.[尺码]=b.[尺码] /* 订单编号 款号 颜色 尺码 数量 JHDD-2011-12-02-00002 699 米色 L 1.00000000 JHDD-2011-12-02-00002 699 米色 M 2.00000000 JHDD-2011-12-02-00002 699 米色 XL 15.00000000 JHDD-2011-12-02-00002 699 米色 XXL NULL */