首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

实际有关问题的请问,

2012-01-13 
实际问题的请教,急!我的数据内容如下:工号打卡日期打卡时间0012006.09.0807:40:090012006.09.0808:09:0000

实际问题的请教,急!
我的数据内容如下:

工号             打卡日期             打卡时间

001             2006.09.08           07:40:09
001             2006.09.08           08:09:00
002             2006.09.08           08:09:00
001             2006.09.09           08:09:09
.
.

如上是上班打卡记录,假设我们上班时间是8点的话,我要找出每天迟到的人,就是说我要找出每天第一次打卡时间是8点之后的人,现在我写的query文,找出的数据都不正确,请大家帮忙,急啊,在线等,


[解决办法]
declare @ta table(工号 varchar(3), 打卡日期 varchar(10), 打卡时间 varchar(8))
insert @ta
select '001 ', '2006.09.08 ', '07:40:09 ' union all
select '001 ', '2006.09.08 ', '08:09:00 ' union all
select '002 ', '2006.09.08 ', '08:09:00 ' union all
select '001 ', '2006.09.09 ', '08:09:09 '

select * from @ta a
where not exists(select 1 from @ta where 工号=a.工号 and 打卡日期=a.打卡日期 and 打卡时间 <a.打卡时间)
and 打卡时间> '08:00:00 '

(4 行受影响)
工号 打卡日期 打卡时间
---- ---------- --------
002 2006.09.08 08:09:00
001 2006.09.09 08:09:09

(2 行受影响)


[解决办法]
--子查询????
--没必要
create table T(工号 varchar(10), 打卡日期 datetime, 打卡时间 varchar(10))
insert T select '001 ', '2006-09-08 ', '07:40:09 '
union all select '001 ', '2006-09-08 ', '08:09:00 '
union all select '002 ', '2006-09-08 ', '08:09:00 '
union all select '001 ', '2006-09-09 ', '08:09:09 '
union all select '001 ', '2006-09-09 ', '18:09:09 '


select 工号,CONVERT(varchar(10),打卡日期,120),
case when min(打卡时间)> '08:00:00 ' then '迟到 ' else '正常 ' end
from t group by 工号, CONVERT(varchar(10),打卡日期,120)

热点排行