合计数小于69的记录如何写SELECT举例:
编号  金额  
1   25
2   12
3   13
5   19
6   20
8   52
===========================现在要求select结果满足条件是:金额的合计数小于65的记录数。结果应该是:
1   25
2   12
3   13应为 25+12+13 = 50 满足条件合计数小于65 请注意:不是组合结果小于65,而是自然存储中的记录顺序金额之和小于65的记录。请问SELECT如何写??谢谢!!

解决方案 »

  1.   

    我要的结果是:select * from table where sum(金额) <= 65但这句是存在语法错误的。
      

  2.   

    --如果想家相加的个数固定:如2个或3个等,参考下例:
    表movie中
    id   mnet
    1     10
    2     20
    3     20
    4     30
    5     40
    6     10
    7     10
    8     20
    9     20
    10    20
    取5条出来,让其mnet总和等于100
    如:id=2,id=4,id=6,id=8,id=9就是一组符合条件的
    请教ING...
    谢谢了~~select  
        a.id,b.id,c.id,d.id,e.id
    from
        movie a,
        movie b,
        movie c,
        movie d,
        movie e
    where
        a.id<b.id and b.id<c.id and c.id<d.id and d.id<e.id
        and
        a.mnet+b.mnet+c.mnet+d.mnet+e.mnet=100
      

  3.   


    create table #tab(bh int,je int)insert into #tab 
    select 1,25
    union all
    select 2,12
    union all
    select 3,13
    union all
    select 5,19
    union all
    select 6,20
    union all
    select 8,52
    select * from #tab a where (select sum(je) from #tab where bh<=a.bh)<65--结果
    bh          je          
    ----------- ----------- 
    1           25
    2           12
    3           13(所影响的行数为 3 行)