求一个SQL语句(Select)

javaxi 2010-01-27 02:24:01
一个表t:
id a b
------------------------
1 100 a
2 80 b
3 200 c
4 500 d
5 100 e
6 300 f

如何按id排序,查询sum(a)值最大值为x的前几条记录?
如果x=512,则结果为
id a b
------------------------
1 100 a
2 80 b
3 200 c

如果x=999,则结果为
id a b
------------------------
1 100 a
2 80 b
3 200 c
4 500 d
5 100 e
...全文
124 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuifengde 2010-01-27
  • 打赏
  • 举报
回复
那你就把id<=a.id倒过来id>=a.id
javaxi 2010-01-27
  • 打赏
  • 举报
回复
就是假如我需要的是id倒序,那这段代码就不通了。。。
javaxi 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chuifengde 的回复:]
SQL codeselect*from[Table] awhere (selectsum(a)from[Table]where id<=a.id)<x
[/Quote]

很好很强大,但少了一个前提。。。。
题目是。。。首先要【按id排序】。。。。
我例子id是顺序的。。。 但实际上可能是没有顺序的。。。
ming460958493 2010-01-27
  • 打赏
  • 举报
回复
declare @i int,@Sum int,@x int
set @x=300
set @i=1
set @Sum=0
select @Sum=sum(金额) from mytb where id<=@i
while @Sum<@x
begin
set @i=@i+1
select @Sum=sum(金额) from mytb where id<@i

end
select * from mytb where id<@i-1
hackervip1988 2010-01-27
  • 打赏
  • 举报
回复

都是牛人
来围观
看看
Hamsic 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sql77 的回复:]
引用 2 楼 chuifengde 的回复:
SQL codeselect*from[Table] awhere (selectsum(a)from[Table]where id <=a.id) <x

晕,我搞复杂了
[/Quote]
-.-你的sql 用什么写的 - -

还是用sqlserver 还是第三方的
jwdream2008 2010-01-27
  • 打赏
  • 举报
回复
学习啊!!
SQL77 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chuifengde 的回复:]
SQL codeselect*from[Table] awhere (selectsum(a)from[Table]where id<=a.id)<x
[/Quote]
晕,我搞复杂了
SQL77 2010-01-27
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :SQL77(只为思齐老)
-- Date :2010-01-27 14:27:50
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb([id] int,[a] int,[b] varchar(1))
insert #tb
select 1,100,'a' union all
select 2,80,'b' union all
select 3,200,'c' union all
select 4,500,'d' union all
select 5,100,'e' union all
select 6,300,'f'
--------------开始查询--------------------------
declare @x int
set @x=512
select * from #tb where id in(
select id from #tb t where (select sum(a) from #tb where id<=t.id)<=512)
----------------结果----------------------------
/*

(所影响的行数为 6 行)

id a b
----------- ----------- ----
1 100 a
2 80 b
3 200 c

(所影响的行数为 3 行)


*/
黄_瓜 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chuifengde 的回复:]
SQL codeselect*from[Table] awhere (selectsum(a)from[Table]where id<=a.id)<x
[/Quote]
黄_瓜 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 javaxi 的回复:]
一个表t:
id  a  b
------------------------
1  100  a
2  80  b
3  200  c
4  500  d
5  100  e
6  300  f

如何按id排序,查询sum(a)值最大值为x的前几条记录?
如果x=512,则结果为
id  a  b
------------------------
1  100  a
2  80  b
3  200  c

如果x=999,则结果为
id  a  b
------------------------
1  100  a
2  80  b
3  200  c
4  500  d
5  100  e
[/Quote]
后面的总和大于900吧?
chuifengde 2010-01-27
  • 打赏
  • 举报
回复
select * from [Table] a where (select sum(a) from [Table] where id<=a.id)<x
jwdream2008 2010-01-27
  • 打赏
  • 举报
回复
select * from TableName where sum(a)<=512

???

34,594

社区成员

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

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