大家好,如何写一条sql,获取如下表中从第几行列ColA的值为Null的,十分感谢ID ColA
====  =======
57 AD
58 AC
59 AY
60 Null
61 Null
62 Null
63 Null
64 Null
65 Null
66 Null

解决方案 »

  1.   

    SELECT *,(SELECT COUNT(1) FROM TB WHERE ID<=T.ID) AS NUM FROM TB T
      

  2.   

    select row_number = 1 + isnull((select count(*) from t  where t.id < d.id),0),
    * from t d 
    where d.ColA is null
    order by d.id
      

  3.   

    select min(no) from 
      (select row_number() over(order by getdate()) no,* from tb) a
       where cola is null
      

  4.   


    稍修改一下
    SELECT *,(SELECT COUNT(1) FROM TB WHERE ID<=T.ID) AS RowNUM FROM  TB T WHERE COLA IS NULL
      

  5.   


    declare @table table (ID int,ColA varchar(2))
    insert into @table
    select 57,'AD' union all
    select 58,'AC' union all
    select 59,'AY' union all
    select 60,null union all
    select 61,null union all
    select 62,null union all
    select 63,null union all
    select 64,null union all
    select 65,null union all
    select 66,nullselect min(ID) from @table where ColA is null 
    /*60*/
      

  6.   

    补充一下,只是要获取为从第几行,开始为Null,返回行号就好
      

  7.   

    declare @table table (ID int,ColA varchar(2))
    insert into @table
    select 57,'AD' union all
    select 58,'AC' union all
    select 59,'AY' union all
    select 60,null union all
    select 61,null union all
    select 62,null union all
    select 63,null union all
    select 64,null union all
    select 65,null union all
    select 66,nullselect min(no) from 
      (select row_number() over(order by getdate()) no,* from @table) a
       where cola is null
    /*
     --------------------
    4
      

  8.   

    结贴了,感谢@ssp2009 和大家的帮助
      

  9.   


    create  table tb(ID int,ColA varchar(2))
    insert into tb
    select 57,'AD' union all
    select 58,'AC' union all
    select 59,'AY' union all
    select 60,null union all
    select 61,null union all
    select 62,null union all
    select 63,null union all
    select 64,null union all
    select 65,null union all
    select 66,nullselect (select count(1)+1 from tb b where b.id<a.id) from tb a 
    where a.id=(select min(id) from tb where ColA is null)/*
                
    ----------- 
    4(所影响的行数为 1 行)
    */