请教一条sql语句的写法,在时间段范围内表中最后一条纪录符合某个条件的所有纪录.

crevenn 2007-12-23 04:28:40
具体问题为:
数据库中kq表(考勤)有ygbh(员工编号)\rq(时间)\bm(部门)....
一般为一天每人会有一条(休日没有)
想查询出在11-21号 到 12-20号范围内考勤纪录最后一条部门 = '系统' 的所有员工的员工编号(ygbh)
因为中途可能有员工会有调动情况(如果前段时间为系统部门,后段时间换到其他部门则不需要).最后一天不一定会有出勤纪录(没办法按照时间来直接取).
请问如何写
...全文
354 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
crevenn 2007-12-24
  • 打赏
  • 举报
回复
select distinct ygbh from kq where rq='2007-12-20' and bm='系统'
这个rq='2007-12-20' 不能保证在查询时间段最后一天有所有员工考勤纪录,意思是
-
时间段内所有员工存在的最后一条考勤纪录所在部门=‘系统’,取出符合条件的员工。
-
好像自己为了表达出这个意思都写糊涂了。呵呵
Limpire 2007-12-24
  • 打赏
  • 举报
回复
select a.* from
(select * from kq t where rq>='2007-11-21' and rq<'2007-12-21' and rq=(select max(rq) from kq where datediff(day,rq,t.rq)=0)) a
inner join
(select distinct ygbh from kq where rq='2007-12-20' and bm='系统') b
on a.ygbh=b.ygbh
crevenn 2007-12-23
  • 打赏
  • 举报
回复
rq>='2007-11-21' and rq<'2007-12-21' rq=(select max(rq) from kq where datediff(day,rq,t.rq)=0)
这里编译不过去
服务器: 消息 170,级别 15,状态 1,行 2
第 2 行: 'rq' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 2
第 2 行: ')' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 4
在关键字 'where' 附近有语法错误。
Limpire 2007-12-23
  • 打赏
  • 举报
回复
select a.* from
(select * from kq t where rq>='2007-11-21' and rq<'2007-12-21' rq=(select max(rq) from kq where datediff(day,rq,t.rq)=0)) a
inner join
(select distinct ygbh from where rq='2007-12-20' and bm='系统') b
on a.ygbh=b.ygbh
crevenn 2007-12-23
  • 打赏
  • 举报
回复
我晕。没这么麻烦吧。我只需要一条或者2条sql语句完成的那种
就是

考勤表中 时间 在输入的时间段范围内
以上的所有纪录中,一个员工会有很多条纪录,他的最后一条纪录中的部门 = ‘系统’
符合上面条件的所有员工的编号
dawugui 2007-12-23
  • 打赏
  • 举报
回复
看不明白,看这个是不是你需要的.
--按某一字段分组取最大(小)值所在行的数据
(爱新觉罗.毓华 2007-10-23于浙江杭州)
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go

--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/

--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/

--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
*/

--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 5 b5b5b5b5b5
*/

--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
b 2 b2b2b2b2
*/

--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
a 3 a3:a的第三个值
b 4 b4b4
b 5 b5b5b5b5b5
*/

34,593

社区成员

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

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