你。
left join 能 出来0 , 10201002,1 。。null,10201002,1 ,null ...关键是null怎么给值啊

解决方案 »

  1.   

    关键是最后1个字段,depcode..怎么变成1和2,,,left join之后都是null....
      

  2.   

    isnull(column,0)
    isnull(column,0)  后面给0 。。就都是0了啊这个是1或者2。。上面只是个例子,数据还很多呢
      

  3.   

    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 
      

  4.   

    with tb as
    (
      select distinct depcode from t2
    )
    , tb2 as
    (
      select wlcode from t1
      except
      select wlcode from t2
    )
    select * from t2
    union all
    select 0, wlcode, depcode
    from tb2,tb
      

  5.   

    select
     isnull(b.qty,0),a.*
    from
    (select wlcode from a cross join (select distinct depcode) as b)a
    left join b on a.wlcode=b.wlcode
      

  6.   

    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
      

  7.   

    select t2.* from t1 
    inner join t2
    on t1.wlcode = t2.wlcode 
    union all
    select 0,a.wlcode,b.depcode from
    (
    select t1.* from t1
    left join t2
    on t1.wlcode = t2.wlcode 
    where t2.qty is null
    ) a cross
    (
      select depcode from t2
    ) b
      

  8.   

    把上面小人鱼的代码,改了一下: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 32,'10201050',2 
    select isnull(t2.qty,0) qty,t1.wlcode,t.depcode
    from #tb1 t1
    inner join 
    (
    select depcode
    from #tb2
    group by depcode
    )t
     on 1= 1
    left join #tb2 t2
           on t1.wlcode = t2.wlcode and
              t.depcode = t2.depcode
    /*
    qty wlcode depcode
    0 10201002 1
    32 10201005 1
    16 10201015 1
    22 10201050 1
    0 10201002 2
    55 10201005 2
    14 10201015 2
    32 10201050 2
    */drop table #tb1 
    drop table #tb2 
      

  9.   

    这个不对,很多depcode.如果是字母呢。
      

  10.   

    加个and a.depcode = b.depcode   我就要这个答案。太感谢了。
      

  11.   

    这个不对,很多depcode.如果是字母呢。以為是固定的,哈哈看小當家的
      

  12.   

    这个不对,很多depcode.如果是字母呢。以為是固定的,哈哈看小當家的呵呵,实在不好意思,上面写错你的昵称了,应该是 小鱼人,一看到你的图片,就想起 路飞了
      

  13.   

    这个不对,很多depcode.如果是字母呢。以為是固定的,哈哈看小當家的呵呵,实在不好意思,上面写错你的昵称了,应该是 小鱼人,一看到你的图片,就想起 路飞了
    ...