为了保住工作,再次发帖恳请高手帮忙关于考勤统计时间的sql语句

Angel008 2010-05-19 06:38:44
非常感谢csw200201在上个帖子中的 的回答 耽误您不少时间了吧 非常谢谢 估计是我没有说清楚吧 现在我也考虑把表再简化一下 我把具体情况说一次 (我现在只统计表里面每天为偶数的记录的时间比如下面的记录,也不考虑进出的情况)
ID 卡号 考勤时间
1 2038375602 2010-03-08 9:25:05
2 2038375602 2010-03-08 10:25:05
3 2038375601 2010-03-08 10:28:05
4 2038375601 2010-03-08 12:28:05
5 2038375601 2010-03-08 17:28:05
6 2038375602 2010-03-08 12:25:05
7 2038375602 2010-03-08 15:25:05
8 2038375603 2010-03-08 9:00:00
9 2038375603 2010-03-08 10:00:00
10 2038375603 2010-03-09 9:00:00
11 2038375603 2010-03-09 10:30:00

得到的结果 应该是 2038375602 2010-03-08 4
2038375603 2010-03-08 1
2038375602 2010-03-09 1.5
就是统计每天的 考勤小时数 考勤次数为奇数的 就不做统计 (我希望的结果是 写一个函数 传一个开始日期 和结束日期 就能自动统计出 这个时间段 每个卡号的 时间段内每天的考勤时间 )
...全文
315 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
csw200201 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 csw200201 的回复:]
引用 8 楼 xys_777 的回复:
竟然没结,难道。。。


I have given up on her - unfortunately. This is not a difficult problem but she's not getting it.
[/Quote]

I am probably a bit too harsh on her. It's very frustrating that when you provide perfectly good solution and the recipient still totally does not get it.

Anyway, Angel, if you are desperate for help, send me a PM and leave me your msn ID.
csw200201 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xys_777 的回复:]
竟然没结,难道。。。
[/Quote]

I have given up on her - unfortunately. This is not a difficult problem but she's not getting it.
dla001 2010-06-03
  • 打赏
  • 举报
回复
不会吧,好运
永生天地 2010-06-03
  • 打赏
  • 举报
回复
竟然没结,难道。。。
uvvvw 2010-05-19
  • 打赏
  • 举报
回复
给你个sql 语句吧。

sql="select 卡号,count(*) 考勤次数,substring(考勤时间,1,10) 日期 from table1 group by 卡号,substring(考勤时间,1,10) where 考勤时间>='"+st+"' and 考勤时间<='"+et+"'"

其中 st是开始时间,et是结束时间
永生天地 2010-05-19
  • 打赏
  • 举报
回复
小小一改

if object_id('tb') is not null drop table tb
go
create table tb(id int identity(1,1),人员卡号 varchar(20), 考勤时间 datetime )

---2
insert tb
select '2038375602',' 2010-03-08 9:25:05' union all
select '2038375602',' 2010-03-08 10:25:05' union all
select '2038375601',' 2010-03-08 10:28:05' union all
select '2038375601',' 2010-03-08 12:28:05' union all
select '2038375601',' 2010-03-08 17:28:05' union all
select '2038375602',' 2010-03-08 12:25:05' union all
select '2038375602',' 2010-03-08 15:25:05' union all
select '2038375603',' 2010-03-08 9:00:00' union all
select '2038375603',' 2010-03-08 10:00:00' union all
select '2038375603',' 2010-03-09 9:00:00' union all
select '2038375603',' 2010-03-09 10:30:00'

alter function f_test(@s datetime,@e datetime)
returns @t table(人员卡号 varchar(50), 考勤时间 datetime ,小时 numeric(4,1))
as
begin
declare @t1 table (rowid int,人员卡号 varchar(50), 考勤时间 datetime )
insert into @t1
select rowid=count(1),a.人员卡号 , a.考勤时间
from tb a left join tb b
on a.人员卡号=b.人员卡号 and a.考勤时间>=b.考勤时间 and datediff(dd,a.考勤时间,b.考勤时间)=0
where a.考勤时间 between @s and @e
group by a.人员卡号 , a.考勤时间
order by a.人员卡号 , a.考勤时间

insert into @t
select 人员卡号=a.人员卡号 + case when max(a.rowid)%2=1 then '该卡刷'+ltrim(max(a.rowid))+'次,请查明问题!'else '' end,
convert(varchar(10),a.考勤时间,120) 考勤时间,
工作时间=sum(case when a.rowid%2=1 then datediff(mi,a.考勤时间,b.考勤时间) else 0 end)*1.0/60
from @t1 a left join @t1 b
on a.人员卡号=b.人员卡号 and a.rowid=b.rowid-1 and datediff(dd,a.考勤时间,b.考勤时间)=0
group by a.人员卡号,convert(varchar(10),a.考勤时间,120)
return
end

select * from dbo.f_test('2010-03-01','2010-03-10')

/*
人员卡号 考勤时间 小时
-------------------------------------------------- ------------------------------------------------------ ------
2038375601该卡刷3次,请查明问题! 2010-03-08 00:00:00.000 2.0
2038375602 2010-03-08 00:00:00.000 4.0
2038375603 2010-03-08 00:00:00.000 1.0
2038375603 2010-03-09 00:00:00.000 1.5

(所影响的行数为 4 行)

*/
Angel008 2010-05-19
  • 打赏
  • 举报
回复
谢谢dawugui的提醒 加了这个就更复杂了 。 并且有时候进了没刷 多刷 很多情况 很难处理的
dawugui 2010-05-19
  • 打赏
  • 举报
回复
我觉得你的表最好加个字段,记录某个打卡时间是进还是出?
Angel008 2010-05-19
  • 打赏
  • 举报
回复
xys_777 非常谢谢你的解答,但是统计的不对 比如说
2038375602 2010-03-08 应该是4个小时 9:25进去,10:25出来 12:25:05又进去了 15:25:05出来的 应该是 1小时 + 3小时 一共4个小时
「已注销」 2010-05-19
  • 打赏
  • 举报
回复
帮你顶上去 UP UP
永生天地 2010-05-19
  • 打赏
  • 举报
回复
记得这个问题我上次也费了不少事,给你写得很清楚,而且包括刷卡次数有问题的
并且逐个情况进行了测试,可是不知道您是否满意。
还是用上次的 思路给你写一遍,如果不行你就说说,怎么改。
---1
if object_id('tb') is not null drop table tb
go
create table tb(id int identity(1,1),人员卡号 varchar(20), 考勤时间 datetime )

---2
insert tb
select '2038375602',' 2010-03-08 9:25:05' union all
select '2038375602',' 2010-03-08 10:25:05' union all
select '2038375601',' 2010-03-08 10:28:05' union all
select '2038375601',' 2010-03-08 12:28:05' union all
select '2038375601',' 2010-03-08 17:28:05' union all
select '2038375602',' 2010-03-08 12:25:05' union all
select '2038375602',' 2010-03-08 15:25:05' union all
select '2038375603',' 2010-03-08 9:00:00' union all
select '2038375603',' 2010-03-08 10:00:00' union all
select '2038375603',' 2010-03-09 9:00:00' union all
select '2038375603',' 2010-03-09 10:30:00'

alter function f_test(@s datetime,@e datetime)
returns @t table(人员卡号 varchar(50), 考勤时间 datetime ,小时 numeric(4,1))
as
begin
declare @t1 table (rowid int,人员卡号 varchar(50), 考勤时间 datetime )
insert into @t1
select rowid=count(1),a.人员卡号 , a.考勤时间
from tb a left join tb b
on a.人员卡号=b.人员卡号 and a.考勤时间>=b.考勤时间 and datediff(dd,a.考勤时间,b.考勤时间)=0
where a.考勤时间 between @s and @e
group by a.人员卡号 , a.考勤时间
order by a.人员卡号 , a.考勤时间

insert into @t
select 人员卡号=a.人员卡号 + case when max(a.rowid)%2=1 then '该卡刷'+ltrim(max(a.rowid))+'次,请查明问题!'else '' end,
convert(varchar(10),a.考勤时间,120) 考勤时间,
工作时间=sum(case when a.rowid%2=0 then datediff(mi,b.考勤时间,a.考勤时间) else datediff(mi,a.考勤时间,b.考勤时间)end)*1.0/60
from @t1 a left join @t1 b
on a.人员卡号=b.人员卡号 and a.rowid=b.rowid+1 and datediff(dd,a.考勤时间,b.考勤时间)=0
group by a.人员卡号,convert(varchar(10),a.考勤时间,120)
return
end

select * from dbo.f_test('2010-03-01','2010-03-10')

/*
人员卡号 考勤时间 小时
-------------------------------------------------- ------------------------------------------------------ ------
2038375601该卡刷3次,请查明问题! 2010-03-08 00:00:00.000 -3.0
2038375602 2010-03-08 00:00:00.000 2.0
2038375603 2010-03-08 00:00:00.000 1.0
2038375603 2010-03-09 00:00:00.000 1.5

(所影响的行数为 4 行)
*/



22,210

社区成员

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

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