我有一表,有两个字段
姓名    数量
刘水    4
王难    6
张三    7
现在求:每个人距离上一个数量,还差几个量?就是求刘水离上一名还差几个量,他差2个,王难离上一名差1个量,张三差0个
这个2,1,0,怎么个弄出来法?不用游标,直接用程序读出来,然后上下减?
这个怎么写呢?

解决方案 »

  1.   

    declare @a int;
    select top 1 @a = shuliang from table where ....,这是最大的那个数,倒排序
    select xingming, @a - shuliang as cha , @a = shuliang from table where ....倒排序思路:先记下最大值,并进行降序计算,这样就可缓存上一个值,作为下一条记录的被减数,一定要注意顺序问题
      

  2.   


    declare @t table([name] varchar(20),num int)insert into @t select '刘水',4
    union all select '王难', 6
    union all select '张三', 7select * from @tselect *,isnull((select top 1 t.num from @t t where t.num>tt.num order by tt.num desc )-tt.num,0) from @t tt
      

  3.   


    declare @t table([name] varchar(20),num int)insert into @t select '刘水',4
    union all select '王难', 6
    union all select '张三', 7select * from @tselect *,isnull((select top 1 t.num from @t t where t.num>tt.num order by tt.num desc )-tt.num,0) result from @t tt
      

  4.   

    ?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 '张三', 7select * from @tselect *,isnull((select top 1 t.num from @t t where t.num>tt.num order by tt.num desc )-tt.num,0) result from @t tt
      

  5.   

    真的不对。是和比上一个总数想比??
    select *,isnull((select top 1 t.sumcount from v_webselecct_sumcount t where t.sumcount>tt.sumcount order by tt.sumcount desc)-tt.sumcount,0) result from v_webselecct_sumcount tt order by sumcount desc根据你的,值是这样的:8118 25 0
    8224 25 0
    8306 22 3
    8219 21 4
    8141 21 4
    8101 21 4
    8131 20 1
    8329 20 1
    8112 18 3
    8103 18 3
    8220 17 4
    8215 17 4
    8302 17 4
    8324 17 4
    这几个值不对啊?
    8306 22 3
    8219 21 4
    8141 21 4
    8101 21 4
    8131 20 1
    8329 20 1
      

  6.   

    select *,isnull((select top 1 t.sumcount from v_webselecct_sumcount t where t.sumcount>tt.sumcount order by tt.sumcount desc)-tt.sumcount,0) result from v_webselecct_sumcount tt order by sumcount descv_webselecct_sumcount 这是一个视图,视图是这样 的:create view  v_webselecct_sumcount
    as
    select bm,count(1) as sumcount  from  dbo.v_webselecct_sendinfo_all where lbmc='success' group by bm 
      

  7.   

    我数据是这样的:
    select bm,count(1) as sumcount  from  dbo.v_webselecct_sendinfo_all where lbmc='成功' group by bm 这个视图执行后为:8101 29
    8102 8
    8103 19
    8105 15
    8106 11
    8107 1
    8108 8
    8110 16
    8111 11
    8112 25
    8113 19
    8117 3
    8118 31
    8120 1
    8124 16
    8125 13
    8126 14
    8128 9
    8130 14
    8131 29
    8132 10
    8133 14
    8141 23
    8143 2
    8201 18
    8202 1
    8203 20
    8205 17
    8207 14
    8208 12
    8210 11
    8213 8
    8215 17
    8216 10
    8217 20
    8218 1
    8219 24
    8220 23
    8223 18
    8224 33
    8227 10
    8232 16
    8234 13
    8238 1
    8241 13
    8302 19
    8303 13
    8304 21
    8306 23
    8308 17
    8310 14
    8311 17
    8312 1
    8313 15
    8314 17
    8315 12
    8319 13
    8320 1
    8324 18
    8325 10
    8327 18
    8329 24
    8330 9
    8332 12
    8333 8
    8335 17
    8343 17
      

  8.   


    select *,isnull((select top 1 min(t.num) from @t t where t.num>tt.num  group by t.num  )-tt.num,0) result from @t tt
    order by num desc
      

  9.   


    select *,isnull((select top 1 min(t.sumcount) from v_webselecct_sumcount t where t.sumcount>tt.sumcount  group by t.sumcount  )-tt.sumcount,0) result from v_webselecct_sumcount tt
    order by sumcount desc
    数据如下:
    8224 39 0
    8118 36 0
    8101 32 0
    8131 32 0
    8112 29 0
    8219 27 0
    8220 27 0
    8141 27 0
    8329 25 0
    8306 25 0
    8327 24 0
    8304 23 0
    8311 22 0
    8223 22 0
    8203 21 0
    8324 21 0
    8217 21 0
    8302 21 0
    8215 20 0
    8113 20 0
    8103 20 0
    8314 19 0
    8335 19 0
    8343 19 0
    8124 19 0
    8205 19 0
    8201 19 0
    8110 18 0
    8105 18 0
    8308 18 0
    8232 18 0
    8313 18 0
    8319 16 0
    8310 16 0
    8126 16 0
    8133 15 0
    8207 15 0
    8241 15 0
    8332 15 0
    8315 15 0
    8130 14 0
    8125 14 0
    8111 13 0
    8208 13 0
    8210 13 0
    8303 13 0
    8234 13 0
    8216 12 0
    8325 12 0
    8330 12 0
    8333 12 0
    8106 12 0
    8132 10 0
    8108 10 0
    8227 10 0
    8102 9 0
    8213 9 0
    8128 9 0
    8107 5 0
    8143 4 0
    8119 4 0
    8206 4 0
    8320 3 0
    8238 3 0
    8117 3 0
    8120 3 0
    8202 3 0
    8218 2 1
    8312 2 1
      

  10.   


    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 *,isnull((select top 1 min(t.num) from @t t where t.num>tt.num  group by t.num  )-tt.num,0) result from @t tt
    order by num desc
      

  11.   

    select bm,count(1) as sumcount from dbo.v_webselecct_sendinfo_all where lbmc='成功' group by bm 是不是这个和数据类弄有关系啊?count(1) as sumcount 这个是什么类型的呢?我一直统计不出正确的数据?
      

  12.   

    是啊,我的也和你的一模一样,只是我的是一个视图而已,视图里也是count(1) as num ,最后和你的一样,
    数据还是不对,郁闷
      

  13.   

    你的图路是正确的,一套在我的视图里就全成0了
    result 就全成0了