日期:2014-05-17 浏览次数:20618 次
declare @vip_sorder table(billid int,bomno varchar(2),materialid varchar(1),mqty int,elemgid varchar(1),dbomno varchar(2),dqty int)
insert into @vip_sorder
select 1 , 'AN' , 'A ', 1 , 'B' , 'BN' , 1 union all
select 1 , 'AN' , 'A' , 1 , 'C' , ' ', 1 union all
select 1 , 'AN' , 'A' , 1 , 'D' , ' ', 1 union all
select 2 , 'BN' , 'B' , 1 , 'E' , 'EN', 1 union all
select 2 , 'BN' , 'B' , 1 , 'F' , '' , 1 union all
select 3 , 'EN' , 'E' , 1 , 'G' , '' , 1 union all
select 4 , 'AK' , 'A' , 1 , 'B' , 'EK' , 1 union all
select 5 , 'EK' , 'B' , 1 , 'J' , '' , 1
select * from @vip_sorder
;with cte as
(
select billid,bomno,materialid,mqty,elemgid,dbomno,dqty from @vip_sorder where materialid='A' and bomno in ('AN','AK')
union all
select cte.billid,cte.bomno,cte.materialid,t.mqty,t.elemgid,t.dbomno,t.dqty from @vip_sorder t,cte
where cte.dbomno=t.bomno
)
select * from cte
where dbomno=''
order by billid,bomno,materialid
/*
billid bomno materialid mqty elemgid dbomno dqty
----------- ----- ---------- ----------- ------- ------ -----------
1 AN A 1 C 1
1 AN A 1 D 1
1 AN A 1 F 1
1 AN A 1 G 1
4 AK A 1 J 1
*/