select a.编码, b.个数 as [个数(200502)]
from (select distinct 编码 from 表) a left join 表 b on a.编码 = b.编码
where b.年月 = '200502'

解决方案 »

  1.   

    create table #t ([id] [int],[name] [char] (4))
    insert into #t(id,name) values(1 , 'null')
    insert into #t(id,name) values(2 , 'null')
    insert into #t(id,name) values(3 , 'null')
    insert into #t(id,name) values(4 , 'null')select A.id, #T.id, isnull(a.id, 'null')
    from A left join #t on A.id = #t.id  where a.年月=200502
      

  2.   

    create table #t ([id] [int],[name] [char] (4))
    insert into #t(id,name) values(1 , 'null')
    insert into #t(id,name) values(2 , 'null')
    insert into #t(id,name) values(3 , 'null')
    insert into #t(id,name) values(4 , 'null')select 编码 as id , 个数 as num into #m from a where 年月 = 200502 /'200502'select #m.id , isnull(#m.num, #t.name)
    from #m left join #t on #m.id = #t.id