意思就是有一张表,记录的是一本书<html>的信息,它们是一样的书,但是批号不一样,如果有人来买书的话,那我要先卖那些先进来的书,就是按我上面的顺序从上往下卖,如果有人要买10本或者小于10本,那我的第一条记录的数量就可以满足了,我只要查询出第一条记录
如果有人要买15本,那我第一条记录只有10本,所以我现在要卖第一条记录的10本还有第二条记录的5本,我要查询出前两条记录。
如果有人要买35本,那我前两条记录加起来才30本不够,那我就要查出加起来大于35本的那几条记录
我的意思能明白么,谢谢了。

解决方案 »

  1.   

    max(最大借书量)
    min(最小借书量)
    select top ((max-min)/10) from tablename
      

  2.   

    --测试环境
    declare @table table(xh int,name char(4),ph char(4),sl int)
    --测试数据
    insert @table select
    1,    'html',   '1111', 10 union all select
    2 ,   'html',   '2222', 20 union all select
    3  ,  'html',   '3333', 30 union all select
    4   , 'html',   '4444', 40 union all select
    5    ,'html',   '5555', 50
    --实现
    select b.xh,b.name,b.ph,b.sl from @table a join @table b
    on  a.xh <= b.xh
    group by b.xh,b.name,b.ph,b.sl
    having sum(a.sl) <= 30 --可以动态修改 /*
    xh          name ph   sl          
    ----------- ---- ---- ----------- 
    1           html 1111 10
    2           html 2222 20(所影响的行数为 2 行)
    */
      

  3.   

    你的问题必须要传入参数(你要读的书数量),然后用when case语句。
      

  4.   

    shuiniu(飞扬的梦) ,你的语句结果是不对的,你试试看,还可以改进么?
      

  5.   

    shuiniu(飞扬的梦)的 having语句就可以了
      

  6.   

    --测试环境
    declare @table table(xh int,name char(4),ph char(4),sl int)
    --测试数据
    insert @table select
    1,    'html',   '1111', 10 union all select
    2 ,   'html',   '2222', 20 union all select
    3  ,  'html',   '3333', 30 union all select
    4   , 'html',   '4444', 40 union all select
    5    ,'html',   '5555', 50
    --实现
    select * from @table
    where xh <=
    (select top 1 b.xh from @table a join @table b
    on  a.xh <= b.xh
    group by b.xh,b.name,b.ph,b.sl
    having sum(a.sl)>=35--可以动态修改 
    )
    /*
    xh          name ph   sl          
    ----------- ---- ---- ----------- 
    1           html 1111 10
    2           html 2222 20
    3           html 3333 30(所影响的行数为 3 行)
    */
      

  7.   

    --测试环境
    declare @table table(xh int,name char(4),ph char(4),sl int)
    --测试数据
    insert @table select
    1,    'html',   '1111', 10 union all select
    2 ,   'html',   '2222', 20 union all select
    3  ,  'html',   '3333', 30 union all select
    4   , 'html',   '4444', 40 union all select
    5    ,'html',   '5555', 50select * from @table a,
    (select xh,(select sum(sl) from @table where xh <= a.xh) sumsl from @table a) b
    where a.xh = b.xh and b.sumsl <=10select * from @table a,
    (select xh,(select sum(sl) from @table where xh <= a.xh) sumsl from @table a) b
    where a.xh = b.xh and b.sumsl <=30select * from @table a,
    (select xh,(select sum(sl) from @table where xh <= a.xh) sumsl from @table a) b
    where a.xh = b.xh and b.sumsl <=60
      

  8.   

    表tmp数据
    xh    name           ph     sl
    1    'html'   '1111' 10 
    2    'html'   '2222' 20 
    3    'html'   '3333' 30 
    4    'html'   '4444' 40 
    5    'html'   '5555' 50
    建函数
    create function testtest(@count int) returns int
    as
    begin 
    declare @xh int
    set @xh=1
    while 1=1
    begin
    if(select sum(sl) from tmp where xh<=@xh)>=@count
    break
    else 
        begin
            set @xh=@xh+1
    continue
        end
    end
    return @xh
    end执行语句
    select * from tmp where xh<=dbo.test(40)
    结果
    1 html 1111 10
    2 html 2222 20
    3 html 3333 30
    函数没有考虑总数少于要求数的情况,自己完善一下
      

  9.   

    SELECT * FROM (SELECT  序号,书名,批号,SUM(数量) OVER (PARTITION BY 书名 ORDER BY 书名, 批号) RM FROM BOOK ) A WHERE A.RM<30;
    试一下看是否支持
      

  10.   

    SELECT * FROM (SELECT  序号,书名,批号,SUM(数量) OVER (PARTITION BY 书名 ORDER BY 书名, 批号) RM FROM BOOK ) A WHERE A.RM<30 and 书名='html'