求SQL,谢谢 我有一个流水帐支出明细表(自动编号,支出时间,支出金额,)里面有好多数据我想依次查询,每次支出金额少于1万的全部记录,的SQL语句,有点类似分页查询的效果 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ?select * from 流水帐支出明细表 where 支出金额<10000 select * from 流水帐支出明细表 where 支出金额<10000先查询出来,再进行循环遍历,或者用游标 ----------------------------------- Author: htl258(Tony)-- Date : 2009-07-23 18:16:25-----------------------------------> 生成测试数据表:tbIf not object_id('[tb]') is null Drop table [tb]GoCreate table [tb]([id] int,[num] decimal(18,1))Insert tbSelect 1,2000 union allSelect 2,3000 union allSelect 3,2129 union allSelect 4,5000 union allSelect 5,1521.5 union allSelect 6,8200Go--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) awhere charindex('-'+rtrim(id)+'-','-'+path+'-')>0/*组别 id num-------------------- ----------- ---------------------------------------1 1 2000.02 1 2000.02 2 3000.03 1 2000.03 2 3000.03 3 2129.04 1 2000.04 2 3000.04 3 2129.04 5 1521.55 1 2000.05 2 3000.05 4 5000.06 1 2000.06 2 3000.06 5 1521.57 1 2000.07 3 2129.08 1 2000.08 3 2129.08 4 5000.09 1 2000.09 3 2129.09 5 1521.510 1 2000.010 4 5000.011 1 2000.011 4 5000.011 5 1521.512 1 2000.012 5 1521.513 2 3000.014 2 3000.014 3 2129.015 2 3000.015 3 2129.015 5 1521.516 2 3000.016 4 5000.017 2 3000.017 4 5000.017 5 1521.518 2 3000.018 5 1521.519 3 2129.020 3 2129.020 4 5000.021 3 2129.021 4 5000.021 5 1521.522 3 2129.022 5 1521.523 4 5000.024 4 5000.024 5 1521.525 5 1521.526 5 1521.526 6 8200.027 6 8200.0(59 行受影响)*/ 为什么插入不行呢 80万条记录插入到MSSQL的表中? 急救---数据库备份问题 求SQL语句? 有个棘手的数据库问题,请教大家了!!! ms-sql数据插入问题,困扰很久了,高人赐教 一个查询的问题 一个SQL语句问题 如何寫程式將FOXPRO數據庫轉為SQL SERVER數據庫 请问powerdesigner8是不是不支持概念模型设计了? 必须声明标量变量... 请帮个忙 MSDE数据丢失的问题
select * from 流水帐支出明细表 where 支出金额<10000
先查询出来,再进行循环遍历,或者用游标
---------------------------------
-- 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 行受影响)
*/