每周的考勤报表查询该咋写?谢谢了!!!

li3365 2004-11-06 03:32:35

有如下表结构
表一:

姓名  刷卡时间
甲 2004-11-1 8:19
甲 2004-11-2 8:19
乙 2004-11-1 8:19
乙 2004-11-2 8:19
乙 2004-11-1 8:19
乙 2004-11-1 8:19
丙 2004-11-3 8:19
丙 2004-11-3 8:19
丁 2004-11-4 8:19

现在想打出每周的考勤报表,格式如下:

2004年第45周总考勤表(10月31日至11月6日)

姓名  星期日上午 星期日下午 ... 星期五上午 星期五下午 星期六上午 星期六下午
甲 √ √ √ √ √ √
乙 √ √ √ √ √
丙 √ √ √ √
丁 √ √ √ √ √ √

例如,甲在11月1日上午有签到记录的话,就在总考勤表甲的“星期一上午”列上打√
有办法写出这个查询吗?
...全文
157 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
li3365 2004-11-06
  • 打赏
  • 举报
回复
真的不愧是老大,肯定天天都在写查询!谢谢了!!
li3365 2004-11-06
  • 打赏
  • 举报
回复
这么快就好了!!,我看看。。
zjcxc 2004-11-06
  • 打赏
  • 举报
回复
select 姓名
,星期日上午=max(case when 周=0 and 上午=1 then '√' else '' end)
,星期日下午=max(case when 周=0 and 上午=0 then '√' else '' end)
,星期一上午=max(case when 周=1 and 上午=1 then '√' else '' end)
,星期一下午=max(case when 周=1 and 上午=0 then '√' else '' end)
,星期二上午=max(case when 周=2 and 上午=1 then '√' else '' end)
,星期二下午=max(case when 周=2 and 上午=0 then '√' else '' end)
,星期三上午=max(case when 周=3 and 上午=1 then '√' else '' end)
,星期三下午=max(case when 周=3 and 上午=0 then '√' else '' end)
,星期四上午=max(case when 周=4 and 上午=1 then '√' else '' end)
,星期四下午=max(case when 周=4 and 上午=0 then '√' else '' end)
,星期五上午=max(case when 周=5 and 上午=1 then '√' else '' end)
,星期五下午=max(case when 周=5 and 上午=0 then '√' else '' end)
,星期六上午=max(case when 周=6 and 上午=1 then '√' else '' end)
,星期六下午=max(case when 周=6 and 上午=0 then '√' else '' end)
from(
select 姓名
,周=(@@datefirst+datepart(weekday,刷卡时间)-1)%7
,上午=case when datepart(hour,刷卡时间)<12 then 1 else 0 end
from 表一
where datepart(week,刷卡时间)=45 --**** 如果只查询指定的周
)a group by 姓名
zjcxc 2004-11-06
  • 打赏
  • 举报
回复
--示例

--示例数据
create table 表一(姓名 varchar(10),刷卡时间 datetime)
insert 表一 select '甲','2004-11-1 8:19'
union all select '甲','2004-11-2 8:19'
union all select '乙','2004-11-1 8:19'
union all select '乙','2004-11-2 8:19'
union all select '乙','2004-11-1 8:19'
union all select '乙','2004-11-1 8:19'
union all select '丙','2004-11-3 8:19'
union all select '丙','2004-11-3 8:19'
union all select '丁','2004-11-4 8:19'
go

select 姓名
,星期日上午=max(case when 周=0 and 上午=1 then '√' else '' end)
,星期日下午=max(case when 周=0 and 上午=0 then '√' else '' end)
,星期一上午=max(case when 周=1 and 上午=1 then '√' else '' end)
,星期一下午=max(case when 周=1 and 上午=0 then '√' else '' end)
,星期二上午=max(case when 周=2 and 上午=1 then '√' else '' end)
,星期二下午=max(case when 周=2 and 上午=0 then '√' else '' end)
,星期三上午=max(case when 周=3 and 上午=1 then '√' else '' end)
,星期三下午=max(case when 周=3 and 上午=0 then '√' else '' end)
,星期四上午=max(case when 周=4 and 上午=1 then '√' else '' end)
,星期四下午=max(case when 周=4 and 上午=0 then '√' else '' end)
,星期五上午=max(case when 周=5 and 上午=1 then '√' else '' end)
,星期五下午=max(case when 周=5 and 上午=0 then '√' else '' end)
,星期六上午=max(case when 周=6 and 上午=1 then '√' else '' end)
,星期六下午=max(case when 周=6 and 上午=0 then '√' else '' end)
from(
select 姓名
,周=(@@datefirst+datepart(weekday,刷卡时间)-1)%7
,上午=case when datepart(hour,刷卡时间)<12 then 1 else 0 end
from 表一
)a group by 姓名
go

--删除测试
drop table 表一

/*--测试结果(自己看)--*/
zjcxc 2004-11-06
  • 打赏
  • 举报
回复
12点区分上下午?

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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