原贴:
http://topic.csdn.net/u/20100322/10/9a0c01d4-f2bb-402d-b189-573feb93f874.html现在值为:
name    量        差值  差名
8117 3 2     
8238 5 1
8206 6 1
8202 7 1
8119 7 1
8120 7 1
8218 7 1
8312 8 2
8227 10 1
8320 10 1
8128 11 2
8107 13 1
我现在想在加一列,这一列(与谁的差值),字段为:差名最终结果为:
name    量        差值  差名
8117 3 2     8238
8238 5 1     8206
8206 6 1     8202
8202 7 1     8312
8119 7 1     8312
8120 7 1     8312
8218 7 1     8312
8312 8 2     8227
8227 10 1     8218
8320 10 1     8218
8128 11 2     8107
8107 13 0     0

解决方案 »

  1.   


    isnull((select top 1 min(t.name) from @t t where t.num>tt.num  group by t.num),tt.name)
      

  2.   


    declare @t table([name] varchar(20),num int)/*
    insert into @t select '刘水',4
    union all select '刘玉', 4
    union all select '王难', 6
    union all select '王四', 6
    union all select '张三', 7
    */
    insert into @t select 8101, 29
    union all select 8102, 8
    union all select 8103 ,19
    union all select 8105 ,15
    union all select 8106 ,11
    union all select 8107 ,1
    union all select 8108 ,8
    union all select 8110 ,16
    union all select 8111 ,11
    union all select 8112 ,25
    union all select 8113 ,19
    union all select 8117 ,3
    union all select 8118 ,31
    union all select 8120 ,1
    union all select 8124 ,16
    union all select 8125 ,13
    union all select 8126 ,14
    union all select 8128 ,9
    union all select 8130 ,14
    union all select 8131 ,29
    union all select 8132 ,10
    union all select 8133 ,14
    union all select 8141 ,23
    union all select 8143 ,2
    union all select 8201 ,18
    union all select 8202 ,1
    union all select 8203 ,20
    union all select 8205 ,17
    union all select 8207 ,14
    union all select 8208 ,12
    union all select 8210 ,11
    union all select 8213 ,8
    union all select 8215 ,17
    union all select 8216 ,10
    union all select 8217 ,20
    union all select 8218 ,1
    union all select 8219 ,24
    union all select 8220 ,23
    union all select 8223 ,18
    union all select 8224 ,33
    union all select 8227 ,10
    union all select 8232 ,16
    union all select 8234 ,13
    union all select 8238 ,1
    union all select 8241 ,13
    union all select 8302 ,19
    union all select 8303 ,13
    union all select 8304 ,21
    union all select 8306 ,23
    union all select 8308 ,17
    union all select 8310 ,14
    union all select 8311 ,17
    union all select 8312 ,1
    union all select 8313 ,15
    union all select 8314 ,17
    union all select 8315 ,12
    union all select 8319 ,13
    union all select 8320 ,1
    union all select 8324 ,18
    union all select 8325 ,10
    union all select 8327 ,18
    union all select 8329 ,24
    union all select 8330 ,9
    union all select 8332 ,12
    union all select 8333 ,8
    union all select 8335 ,17
    union all select 8343 ,17
    select * from @t order by num desc select COUNT(1) from @t group by [num] select *,isnull((select top 1 min(t.num) from @t t where t.num>tt.num  group by t.num  )-tt.num,0) result,
    isnull((select top 1 min(t.name) from @t t where t.num>tt.num  group by t.num),tt.name)
     from @t tt
    order by num desc
      

  3.   

    老大,解决了,谢谢,我是这样写的:不知对不:select *,isnull((select top 1 min(t.num) from @t t where t.num>tt.num  group by t.num  )-tt.num,0) result,
    isnull((select top 1 bm from @t t where t.num>tt.num  ),0) result2
     from @t tt
      

  4.   

    group by t.num 用这个好像不对,报错