快速查询表T1,字段B=0、字段A和值<=5000 按字段ID顺序排列最前面的记录, 并得到这些记录的最大ID和最小ID???

yphy 2007-12-12 09:43:42
快速查询表T1,字段B=0、字段A和值<=5000 按字段ID顺序排列最前面的记录, 并得到这些记录的最大ID和最小ID???

比如工资表T1:

Name Sex Age Class Paid Salary ID

a f 22 1 0 1000 1
aa m 20 2 0 1500 2
b m 23 2 0 1500 3
cs f 21 2 1 1200 4
aw f 22 1 0 1200 5
add f 22 1 1 1000 6
......

Paid 0: 未付, 1: 已付

要求快速查出,没有支付的且排在最前的,Salary的和值<=5000的记录,也就是再加上其后的一条记录和值就超出了100000

例如上表,应该是
Name Sex Age Class Paid Salary ID

a f 22 1 0 1000 1
aa m 20 2 0 1500 2
b m 23 2 0 1500 3


目的是找到还没发工资的人员,但目前资金有限,最多只能发一小部分。一定要“快速”,越快越好。

...全文
310 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dobear_0922 2007-12-12
  • 打赏
  • 举报
回复
如果要提高效率,可以分两句来执行,,,
create table T1(Name nvarchar(32), Sex char(1), Age int, Class int, Paid int, Salary int, ID int)
insert T1 select 'a', 'f', 22, 1, 0, 1000, 1
union all select 'aa', 'm', 20, 2, 0, 1500, 2
union all select 'b', 'm', 23, 2, 0, 1500, 3
union all select 'cs', 'f', 21, 2, 1, 1200, 4
union all select 'aw', 'f', 22, 1, 0, 1200, 5
union all select 'add', 'f', 22, 1, 1, 1000, 6

--定义一个@MaxID以提高效率
declare @MaxID int
set @MaxID=(select TOP 1 ID from T1
where Paid=0 and (select sum(Salary) from T1 t where ID<=T1.ID and Paid=0)>5000
order by ID)

--查没有发工资,且和计不超过5000的
select * from T1
where ID<@MaxID and Paid=0


drop table T1

/*
Name Sex Age Class Paid Salary ID
-------------------------------- ---- ----------- ----------- ----------- ----------- -----------
a f 22 1 0 1000 1
aa m 20 2 0 1500 2
b m 23 2 0 1500 3

(3 row(s) affected)
*/
-狙击手- 2007-12-12
  • 打赏
  • 举报
回复
--try
select *
from T1 t
where Paid=0 and (select sum(Salary) from T1 where id<=t.id and Paid=0)<=5000
lwl0606 2007-12-12
  • 打赏
  • 举报
回复
select *,(select  sum( Salary ) from t1 where Paid=0 and ID<=A.id ) AS A
from t1 A
where Paid=0 AND (select sum( Salary ) from t1 where Paid=0 and ID<=A.id )<=500
order by ID
dobear_0922 2007-12-12
  • 打赏
  • 举报
回复
create table T1(Name nvarchar(32), Sex char(1), Age int, Class int, Paid int, Salary int, ID int)
insert T1 select 'a', 'f', 22, 1, 0, 1000, 1
union all select 'aa', 'm', 20, 2, 0, 1500, 2
union all select 'b', 'm', 23, 2, 0, 1500, 3
union all select 'cs', 'f', 21, 2, 1, 1200, 4
union all select 'aw', 'f', 22, 1, 0, 1200, 5
union all select 'add', 'f', 22, 1, 1, 1000, 6

--查没有发工资,且和计不超过5000的
select * from T1
where Paid=0 and (select sum(Salary) from T1 t where ID<=T1.ID and Paid=0)<=5000

--查最小、最大ID
select MinID=min(ID), MaxID=max(ID) from T1
where Paid=0 and (select sum(Salary) from T1 t where ID<=T1.ID and Paid=0)<=5000

drop table T1

/*
Name Sex Age Class Paid Salary ID
-------------------------------- ---- ----------- ----------- ----------- ----------- -----------
a f 22 1 0 1000 1
aa m 20 2 0 1500 2
b m 23 2 0 1500 3

(3 row(s) affected)

MinID MaxID
----------- -----------
1 3

(1 row(s) affected)
*/
dobear_0922 2007-12-12
  • 打赏
  • 举报
回复
create table T1(Name nvarchar(32), Sex char(1), Age int, Class int, Paid int, Salary int, ID int)
insert T1 select 'a', 'f', 22, 1, 0, 1000, 1
union all select 'aa', 'm', 20, 2, 0, 1500, 2
union all select 'b', 'm', 23, 2, 0, 1500, 3
union all select 'cs', 'f', 21, 2, 1, 1200, 4
union all select 'aw', 'f', 22, 1, 0, 1200, 5
union all select 'add', 'f', 22, 1, 1, 1000, 6

--try
select * from T1
where Paid=0 and (select sum(Salary) from T1 t where ID<=T1.ID and Paid=0)<=5000

drop table T1

/*
Name Sex Age Class Paid Salary ID
-------------------------------- ---- ----------- ----------- ----------- ----------- -----------
a f 22 1 0 1000 1
aa m 20 2 0 1500 2
b m 23 2 0 1500 3

(3 row(s) affected)
*/
playwarcraft 2007-12-12
  • 打赏
  • 举报
回复
目的是找到还没发工资的人员,但目前资金有限,最多只能发一小部分。

===================
強烈要求先發完工資!
dobear_0922 2007-12-12
  • 打赏
  • 举报
回复
--try
select * from T1
where Paid=0 and (select sum(Salary) from T1 t where ID<=T1.ID and Paid=0)<=5000
fwacky 2007-12-12
  • 打赏
  • 举报
回复
有点晕!
dawugui 2007-12-12
  • 打赏
  • 举报
回复
我也没看明白,先写个所谓的字段A,B什么的.
dawugui 2007-12-12
  • 打赏
  • 举报
回复
select * from t1 where 字段B=0、字段A<=5000 order by id

select top 1 id as 最大ID from t1 where 字段B=0、字段A<=5000 order by id desc
select top 1 id as 最小ID from t1 where 字段B=0、字段A<=5000 order by id
fcuandy 2007-12-12
  • 打赏
  • 举报
回复
看了半天看不明.
难道又是过滤后每个class取最小id的一条记录这种东东?
内容概要:本文研究了一种基于遗传算法的新型异构分布式系统任务调度算法,旨在解决异构计算环境中任务分配与调度的复杂优化问题。通过Matlab代码实现该算法,充分利用遗传算法强大的全局搜索能力和鲁棒性,对任务执行时间、资源利用率、系统负载均衡等关键性能指标进行综合优化,有效提升分布式系统的整体运行效率与稳定性。研究详细阐述了算法的整体架构设计、染色体编码策略、适应度函数构造、选择机制以及交叉与变异等遗传操作的实现细节,并通过大量仿真实验验证了所提出算法相较于传统调度方法在收敛速度、解的质量和调度性能方面的显著优越性。; 适合人群:具备一定编程基础和优化算法理论知识,从事分布式计算、高性能计算、云计算资源调度或智能优化算法研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于高性能计算、云计算、边缘计算等异构计算平台中的任务调度优化,提升资源利用效率;②为研究人员提供遗传算法在复杂组合优化问题中应用的完整实现案例,深化对智能优化算法设计原理与仿真实践的理解; 阅读建议:建议读者结合提供的Matlab代码深入研读,重点理解适应度函数的设计逻辑与遗传算子的参数调优策略,并可通过更换不同的任务集和系统模型来测试算法的泛化能力与鲁棒性。
参考李林凤等(2025)一文关于农村劳动力人均受教育年限指标的构建与计算方法,整理了中国31个省份总体、分性别的农村人均受教育年限数据,具体计算方法如下: 农村劳动力人均受教育年限 = (农村未上过学人数 × 1+小学学历人数 × 6+初中学历人数 × 9+高中和中专学历人数 ×12+大专及以上学历人数 × 16)/农村6岁及以上总人口 相关数据:各地区、分性别人均受教育年限数据 一、数据介绍 数据名称:中国各省农村人均受教育年限 数据范围:全国31个省份 时间范围:2006-2024年 样本数量:590条 数据来源:《中国人口和就业统计年鉴》、《中国劳动统计年鉴》 二、数据指标 年份 省份 省份代码 农村人均受教育年限 男性-农村人均受教育年限 女性-农村人均受教育年限 6岁及以上人口 6岁及以上人口_男 6岁及以上人口_女 未上过学人口 未上过学人口_男 未上过学人口_女 小学人口 小学人口_男 小学人口_女 初中人口 初中人口_男 初中人口_女 高中人口 高中人口_男 高中人口_女 大专及以上人口 大专及以上人口_男 大专及以上人口_女 三、参考文献 [1]李林凤,刘杨,杨亦民.种业创新驱动农村产业融合的作用机制与空间分异效应[J].广东财经大学学报,2025,40(6):97-109. [2]徐小阳,李洁,金丽馥.普惠金融对农村教育贫困的纾解效应[J].中国农村经济,2020,(9):41-64.

22,296

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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