我有一个流水帐支出明细表(自动编号,支出时间,支出金额,)
里面有好多数据
我想依次查询,每次支出金额少于1万的全部记录,的SQL语句,有点类似分页查询的效果

解决方案 »

  1.   

    ?
    select * from 流水帐支出明细表 where 支出金额<10000
      

  2.   

    select * from 流水帐支出明细表 where 支出金额<10000
    先查询出来,再进行循环遍历,或者用游标
      

  3.   


    ---------------------------------
    --  Author: htl258(Tony)
    --  Date  : 2009-07-23 18:16:25
    ---------------------------------
    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([id] int,[num] decimal(18,1))
    Insert tb
    Select 1,2000 union all
    Select 2,3000 union all
    Select 3,2129 union all
    Select 4,5000 union all
    Select 5,1521.5 union all
    Select 6,8200
    Go
    --Select * from tb-->SQL查询如下:
    ;with t as
    (
        select *,path=cast(id as varchar(8000)),total=num from tb
        union all
        select b.id,b.num,a.path+'-'+rtrim(b.id),cast(a.total+b.num as decimal(18,1))
        from t a join tb b on a.id<b.id and a.total<=10000
    )
    select 组别=dense_rank()over(order by path),id,num 
    from tb,(
        select path 
    from t 
    where total<=10000
    ) a
    where charindex('-'+rtrim(id)+'-','-'+path+'-')>0
    /*
    组别                   id          num
    -------------------- ----------- ---------------------------------------
    1                    1           2000.0
    2                    1           2000.0
    2                    2           3000.0
    3                    1           2000.0
    3                    2           3000.0
    3                    3           2129.0
    4                    1           2000.0
    4                    2           3000.0
    4                    3           2129.0
    4                    5           1521.5
    5                    1           2000.0
    5                    2           3000.0
    5                    4           5000.0
    6                    1           2000.0
    6                    2           3000.0
    6                    5           1521.5
    7                    1           2000.0
    7                    3           2129.0
    8                    1           2000.0
    8                    3           2129.0
    8                    4           5000.0
    9                    1           2000.0
    9                    3           2129.0
    9                    5           1521.5
    10                   1           2000.0
    10                   4           5000.0
    11                   1           2000.0
    11                   4           5000.0
    11                   5           1521.5
    12                   1           2000.0
    12                   5           1521.5
    13                   2           3000.0
    14                   2           3000.0
    14                   3           2129.0
    15                   2           3000.0
    15                   3           2129.0
    15                   5           1521.5
    16                   2           3000.0
    16                   4           5000.0
    17                   2           3000.0
    17                   4           5000.0
    17                   5           1521.5
    18                   2           3000.0
    18                   5           1521.5
    19                   3           2129.0
    20                   3           2129.0
    20                   4           5000.0
    21                   3           2129.0
    21                   4           5000.0
    21                   5           1521.5
    22                   3           2129.0
    22                   5           1521.5
    23                   4           5000.0
    24                   4           5000.0
    24                   5           1521.5
    25                   5           1521.5
    26                   5           1521.5
    26                   6           8200.0
    27                   6           8200.0(59 行受影响)
    */