22,209
社区成员
发帖
与我相关
我的任务
分享
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)
*/
--try
select *
from T1 t
where Paid=0 and (select sum(Salary) from T1 where id<=t.id and Paid=0)<=5000
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
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)
*/
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)
*/
--try
select * from T1
where Paid=0 and (select sum(Salary) from T1 t where ID<=T1.ID and Paid=0)<=5000
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