日期:2014-05-17 浏览次数:20657 次
create table #tb1 (wlcode varchar(50))
create table #tb2 (qty int,wlcode varchar(50),depcode varchar(2))
insert into #tb1
select '10201002' union all
select '10201005' union all
select '10201015' union all
select '10201050'
insert into #tb2
select 32,'10201005',1 union all
select 16,'10201015',1 union all
select 22,'10201050',1 union all
select 55,'10201005',2 union all
select 14,'10201015',2 union all
select 31,'10201050',2
select qty= case when c.qty IS NULL then 0 else c.qty end ,ab.wlcode ,ab.number from
(select a.wlcode ,b.number from #tb1 a join master .dbo.spt_values b on b.number <3 and b.number >0
where b.type='P' ) ab left join #tb2 c on ab.number =c.depcode and ab.wlcode =c.wlcode
order by ab.number ,ab.wlcode
drop table #tb1
drop table #tb2
select
isnull(b.qty,0),a.*
from
(select * from a cross join (select distinct depcode from b) as b) as a
left join b on a.wlcode=b.wlcode