问题1
A表   code      name 
   12         张三
   13         李四
   14         王五B表
    code         num    strat 
     12          32       1
     13          34       1
     15          23       1
     12          31       0组合起来之后显示成这样   code    name     num
   12      张三    32
   13      李四     34
   15      null     23
                         这里需要有B表strat为1的 就算A表没有也要  但是为0的就不需要了
 select * from a right join b on a.code=b.code and b.strat=1 我这个语句还是有0的 不知道怎么改
 问题2B表这里我建了一个计划自动修改的
     看上面的数据可以看出来B表里面code=12有两条记录
  那我就想自动把第一个code=12的num(strat=1)改成第2个code=12的num (strat=0)
  

解决方案 »

  1.   

    1.SELECT B.CODE,A.NAME,A.NUM
    FROM B 
    INNER JOIN A ON B.CODE=A.CODE AND B.STRAT=1
    第二个,如果同时对同一个CODE插入了1和0的怎么处理
      

  2.   


    问题2B表这里我建了一个计划自动修改的
         看上面的数据可以看出来B表里面code=12有两条记录
      那我就想自动把第一个code=12的num(strat=1)改成第2个code=12的num (strat=0)也就是通过这个步骤 
     B表的数据为
    code            num                strat 
         12          31(这里改了)       1
         13          34                  1
         15          23                  1
         12          31                  0
      

  3.   


    拿第一个start =1的修改就可以了!
      

  4.   

    第一个还是不对  我B表里面code=15的 没有出来!
      

  5.   


    --> 测试数据: ##a
    if object_id('tempdb.dbo.##a') is not null drop table ##a
    create table ##a (code int,[name] varchar(4))
    insert into ##a
    select 12,'张三' union all
    select 13,'李四' union all
    select 14,'王五'select * from ##a
    --> 测试数据: ##b
    if object_id('tempdb.dbo.##b') is not null drop table ##b
    create table ##b (code int,num int,strat int)
    insert into ##b
    select 12,32,1 union all
    select 13,34,1 union all
    select 15,23,1 union all
    select 12,31,0select * from ##bselect b.code,a.name,b.num 
    from ##b b left join ##a a on a.code=b.code 
    where  b.strat=1/*
    code        name num
    ----------- ---- -----------
    12          张三   32
    13          李四   34
    15          NULL 23(3 行受影响)*/
      

  6.   

    SELECT B.CODE,A.NAME,A.NUM
    FROM B 
    right outer JOIN A ON B.CODE=A.CODE AND B.STRAT=1
      

  7.   


    1.
    select * from A
    right join B on A.code=B.code
    where B.strat=1code name code num strat
    12 张三 12 32 1
    13 李四 13 34 1
    NULL NULL 15 23 12.
    update B
    set num=s0.num
    from B s1,(select * from B where strat=0) s0 
    where s1.strat=1 and s1.code=s0.codecode num strat
    12 31 1
    13 34 1
    15 23 1
    12 31 0
      

  8.   


    update aa set num=bb.num
    from b aa inner join b bb on aa.code=bb.code
    where aa.strat=1 and bb.strat=0
      

  9.   


    --问题2
    select * from ##b update  ##b  set num=b.num 
    from ##b a inner join (
    select code,num from (
    select ROW_NUMBER() over (order by code) as rownumber 
    ,* from ##b t1 where strat=0 
    and Exists (select code from ##b where code=t1.code group by code
    having count(*)>1)
    )t2 where t2.rownumber=1
    )b on a.code=b.codeselect * from ##b /*
    code        num         strat
    ----------- ----------- -----------
    12          32          1
    13          34          1
    15          23          1
    12          31          0(4 行受影响)(2 行受影响)
    code        num         strat
    ----------- ----------- -----------
    12          31          1
    13          34          1
    15          23          1
    12          31          0(4 行受影响)
    */