求SQL语句

LL891230 2011-04-04 04:05:51

id point
1 5
2 6
3 7
4 8
5 10
如何写Sql语句定位到recordPoint相加等于18的位置,找到表id
前提不能用游标
...全文
68 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mabailin 2011-04-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 happycell188 的回复:]

SQL code
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
id int,
point int
)
go
--插入测试数据
insert into tb select 1,5
union all select 2,6
union all select ……
[/Quote]

CSDN真能学不少东西,我还在拿变量和循环做
LL891230 2011-04-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 happycell188 的回复:]
select * from (
select *,num=(select sum(point) from tb where id<=t.id) from tb t
)t where num=18
[/Quote]牛!
喜-喜 2011-04-04
  • 打赏
  • 举报
回复
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
id int,
point int
)
go
--插入测试数据
insert into tb select 1,5
union all select 2,6
union all select 3,7
union all select 4,8
union all select 5,10
go
--代码实现

select * from (
select *,num=(select sum(point) from tb where id<=t.id) from tb t
)t where num=18

/*测试结果

id point num
---------------------
3 7 18

(1 行受影响)
*/
LL891230 2011-04-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xuam 的回复:]
不是很明确,你是想找到3和5?
[/Quote]我是想找到id=3的那条
xuam 2011-04-04
  • 打赏
  • 举报
回复
不是很明确,你是想找到3和5?

34,594

社区成员

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

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