语无伦次!SIGNATURE:----------------------------------------------------------------------------------------

解决方案 »

  1.   

    use tempdb
    go 
    create table tb(id int)
    insert tb
    select 1 union all
    select 2 union all
    select 3 union all 
    select 2
    create table #t(num int identity (1,1),id int)
    insert into #t select * from tb
    select a.id ,
    case a.id when (select id from #t where num in (select max(num)from #t)) then 0
    else
       (
    select count(*)  from #t b 
    where  b.id != a.id 
     and b.num >=a.num+1 
      and b.num <= (select max(num)from #t) 
    )
    end
             as 遗漏次数
    from #t a--结果:
    id 遗漏次数
    1 3
    2 0
    3 1
    2 0
      

  2.   


    use tempdb
    go 
    create table tb(id int)
    insert tb
    select 1 union all
    select 2 union all
    select 3 union all 
    select 2
    create table #t(num int identity (1,1),id int)
    insert into #t select * from tb
    select a.id ,
    case a.id when (select id from #t where num in (select max(num)from #t)) then 0
    else
       (
    select count(*)  from #t b 
    where  b.id != a.id 
     and b.num >=a.num+1 
      and b.num <= (select max(num)from #t) 
    )
    end
             as 遗漏次数
    from #t a--结果:
    id 遗漏次数
    1 3
    2 0
    3 1
    2 0
      

  3.   


    use tempdb
    go 
    create table tb(id int)
    insert tb
    select 1 union all
    select 2 union all
    select 3 union all 
    select 5 union all 
    select 3 union all
    select 2 union all
    select 4 
    declare @t table(num int identity (1,1),id int)
    insert into @t select * from tb
    select  
    a.id ,
    case a.id when (select id from @t where num in (select max(num)from @t)) then 0
    else
       (
    select count(*)  from @t b 
    where  b.id != a.id 
     and b.num >=a.num+1 
      and b.num <= (select max(num)from @t) 
    )
    end
             as 遗漏次数
    from (
    select max(num) as num ,id from @t  group by id 
    ) as a--
    1 6
    2 1
    3 2
    4 0
    5 3