求解:时间比较问题

foxkla 2010-04-18 03:27:27
求解:

现在又如下两张表

表manulSign 签卡时间表

SignId UserId SignTime
1 admin 2007-12-06 07:36:14.000
2 admin 2007-12-06 10:36:29.057

表workTime 标准上班时间表

WorkTimeId OnDutyTime
1 8:30:00

要用签卡时间SignTime和OnDutyTime做比较 统计迟到人数 怎么转换 怎么比较?
...全文
56 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
心中的彩虹 2010-04-18
  • 打赏
  • 举报
回复

if object_id('[manulSign]') is not null drop table [manulSign]
go
create table [manulSign]([SignId] int,[UserId] varchar(5),[SignTime] datetime)
insert [manulSign]
select 1,'admin','2007-12-06 07:36:14.000' union all
select 2,'admin','2007-12-06 10:36:29.057'
if object_id('[workTime]') is not null drop table [workTime]
go
create table [workTime]([WorkTimeId] int,[OnDutyTime] varchar(10))
insert [workTime]
select 1,'08:30:00'


select count(SignId) 迟到人数 from manulSign,workTime
where SignTime>=cast('2007-12-06 '+OnDutyTime as datetime)

select UserId,count(SignId) 迟到次数 from manulSign,workTime
where convert(char(8),SignTime,114)>=OnDutyTime
group by UserId

--结果
迟到人数
1


dawugui 2010-04-18
  • 打赏
  • 举报
回复
create table [manulSign]([SignId] int,[UserId] varchar(5),[SignTime] datetime)
insert [manulSign]
select 1,'admin','2007-12-06 07:36:14.000' union all
select 2,'admin','2007-12-06 10:36:29.057'
if object_id('[workTime]') is not null drop table [workTime]
go
create table [workTime]([WorkTimeId] int,[OnDutyTime] varchar(10))
insert [workTime]
select 1,'08:30:00'

select 迟到人数 = count(1) from manulSign m, workTime n where convert(varchar(8) , m.SignTime , 114) > n.OnDutyTime

drop table manulSign , workTime

/*
迟到人数
-----------
1

(所影响的行数为 1 行)
*/
百年树人 2010-04-18
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[manulSign]') is not null drop table [manulSign]
go
create table [manulSign]([SignId] int,[UserId] varchar(5),[SignTime] datetime)
insert [manulSign]
select 1,'admin','2007-12-06 07:36:14.000' union all
select 2,'admin','2007-12-06 10:36:29.057'
if object_id('[workTime]') is not null drop table [workTime]
go
create table [workTime]([WorkTimeId] int,[OnDutyTime] varchar(10))
insert [workTime]
select 1,'08:30:00'

---查询---
select a.*,
case when convert(varchar(8),SignTime,108)>b.OnDutyTime then '迟到' else '正常' end
from manulSign a,
workTime b

---结果---
SignId UserId SignTime
----------- ------ ----------------------- ----
1 admin 2007-12-06 07:36:14.000 正常
2 admin 2007-12-06 10:36:29.057 迟到

(2 行受影响)
htl258_Tony 2010-04-18
  • 打赏
  • 举报
回复
下班范围呢?

22,209

社区成员

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

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