取到“和”小于500的明细

chuifeng2012 2013-04-03 03:50:01
做一个测试表
create table #t
(
isid int primary key identity(1,1),
amount decimal(18,2)
)
go

insert into #t values(23.43)
insert into #t values(213.43)
insert into #t values(523.3)
insert into #t values(23.4)
insert into #t values(263.23)
insert into #t values(223.43)
insert into #t values(223.43)
insert into #t values(293.43)
insert into #t values(223.3)
insert into #t values(23.16)

drop table #t

请问 大家如何才能按照isid顺序由小到大 取到“和”小于 500的明细。此表结果就是取到前两条
就是按照顺序来取到连续数据和小于500的明细

分少 请各位帮帮忙
...全文
464 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuifeng2012 2013-04-17
  • 打赏
  • 举报
回复
上面说的不对不对,我的意思是
select * from #t as a where exists(select 1 from #t where isid<=a.isid having sum(amount)<1000)

这个代码当第一条就大于“和”的时候他不显示,怎样才能显示,显然 1023.43+(-9213.43) 小于1000
chuifeng2012 2013-04-17
  • 打赏
  • 举报
回复
引用 1 楼 roy_88 的回复:
SQL code?1select * from #t as a where exists(select 1 from #t where isid<=a.isid having sum(amount)<500)
create table #t ( isid int primary key identity(1,1), amount decimal(18,2) ) go insert into #t values(1023.43) insert into #t values(-9213.43) insert into #t values(523.3) insert into #t values(23.4) insert into #t values(-263.23) insert into #t values(223.43) insert into #t values(223.43) insert into #t values(293.43) insert into #t values(223.3) insert into #t values(23.16) select * from #t as a where exists(select 1 from #t where isid<=a.isid having sum(amount)<1000) drop table #t 请问版主我将数据改了一下 第一条即超过了“和”,但是这个语句会继续筛选下面的语句。怎么让他停止。比如第一行>1000以后就不往下筛选了
zhangbei59 2013-04-04
  • 打赏
  • 举报
回复
isid<=a.isid 巧妙的排序方法。在exists里面 屡试不爽。
chuifeng2012 2013-04-03
  • 打赏
  • 举报
回复
强,我看了四五遍还没没理解明白你给的语句。 继续学习,非常感谢 roy_88
中国风 2013-04-03
  • 打赏
  • 举报
回复
引用 3 楼 chuifeng2012 的回复:
请教 roy_88 结果没问题。 完全没必要 用类似order by 之类的排序吗?
没有必要,在isid<=a.isid 这一段是条件
chuifeng2012 2013-04-03
  • 打赏
  • 举报
回复
请教 roy_88 结果没问题。 完全没必要 用类似order by 之类的排序吗?
chuifeng2012 2013-04-03
  • 打赏
  • 举报
回复
引用 1 楼 roy_88 的回复:
SQL code?1select * from #t as a where exists(select 1 from #t where isid<=a.isid having sum(amount)<500)
版主速度啊 ,先谢过 我试试去
中国风 2013-04-03
  • 打赏
  • 举报
回复
select * from #t as a where exists(select 1 from #t where isid<=a.isid having sum(amount)<500)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧