从A地到湘潭车费为50,从湘潭到长沙为20,再从长沙到武昌为50......
         id    name  money
         1     湘潭   50
         2     长沙   20
         3     武昌   50
         4     武汉   30
         5     深圳   80
         6     太原   110
         7     北京   70
   如果有250元,想知道能到的站的编号,名称应该用什么SQL语句呢?

解决方案 »

  1.   

    思路是这样的,生成新列出来,变成这样子的:
             id    name  money  newfield
             1     湘潭   50       50
             2     长沙   20       70
             3     武昌   50        120
             4     武汉   30        150
             5     深圳   80        230 
             6     太原   110       340
             7     北京   70        410
    然后就好做了.....
      

  2.   

    create table cs(id int,name varchar(100),money int)
     insert into cs select         1,     '湘潭' ,  50
    union all select          2   ,  '长沙' ,  20
     union all select           3 ,    '武昌' ,  50
      union all select          4 ,    '武汉' ,  30
     union all select           5  ,   '深圳' ,  80
      union all select          6  ,   '太原'  , 110
      union all select          7 ,    '北京' ,  70select top 1 * from (select * ,aa=(select sum(money) from cs where a.id>=id) from cs a) b where aa<=250 order by aa desc
      

  3.   

    此类似问题,此算法的思路,我的blog里有。。
    标题:
    求某個數據的遞增和(多一列)