表A:
state  start1  end1   start2    end2    start3    end3   start4  end4   start5   end5   start6   end6  
   1      1        1    null     null      null    null    null    null    null   null    null    null  
   1      1        1     1        1        null    null    null    null    null   null    null    null  
   0      1        1     1       null      null    null    null    null    null   null    null    null 
说明:
如果(start1,end1)(start2,end2)…  每对中一个为1一个为null的则state=0 else 1
列数固定时如何写sql语句
列数不固定是怎么写
多谢!

解决方案 »

  1.   

    如果(start1,end1)(start2,end2)…  每对中一个为1一个为null的则state=0 else 1 这句话啥意思?解释下
      

  2.   

    就是在每一行里面 判断 (start1  end1)  (start2    end2)    (start3    end3)  (start4  end4)  (start5  end5)  (start6  end6)
    分为六组,每一组里面只有在同为1 或者同为Null 时 state=1 否则为0
    多谢高手!  
      

  3.   

    你的意思是把多列拆成行像这样?state  start1  end1
    0      1       null
    1      1       1
    ...
      

  4.   

    也可以那么理解,
    要保证(start1  end1)  (start2    end2)    (start3    end3)  (start4  end4)  (start5  end5)  (start6  end6) 
    六组总最后都为1 则最终的state为1 else 0
      

  5.   

    create table test0609(state int,start1 int,end1 int,start2 int,end2 int,start3 int,end3 int)
    insert into test0609
    select 1,1,1,null,null,null,null union all
    select 1,1,1,1,1,null,null union all
    select 0,1,1,1,null,null,null
    godeclare @x varchar(500),@xx varchar(500)
    declare @i int
    declare @y intset @x=''
    set @i=1
    -- select @x='select state 'select @y=(count(1)-1)/2 from syscolumns where id=object_id('test0609')
    while @i<=@y
      begin
    select @x=@x+'isnull(start'+cast(@i as varchar)+',0)=isnull(end'+cast(@i as varchar)+',0) '
    if(@i<>@y)
    select @x=@x+'and '
    set @i=@i+1
      endselect @xx='select * from test0609 where '+@xexec  (@xx) 
    /*
    state       start1      end1        start2      end2        start3      end3
    ----------- ----------- ----------- ----------- ----------- ----------- -----------
    1           1           1           NULL        NULL        NULL        NULL
    1           1           1           1           1           NULL        NULL(2 行受影响)*/