34,590
社区成员
发帖
与我相关
我的任务
分享
create table tb(id int,员工编号 varchar(10),姓名 varchar(10),分公司 varchar(20),部门 varchar(20) , 刷卡日期 datetime,刷卡时间 datetime)
insert into tb select 3135 ,'00068', '罗1', '广州公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:28:56.000'
insert into tb select 3136 ,'00188', '李2', '深圳公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:29:58.000'
insert into tb select 3137 ,'00305', '刘3', '广州公司', '前厅部', '2011-10-11 00:00:00.000', '1900-01-01 16:35:22.000'
insert into tb select 3138 ,'00260', '骆4', '北京公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:35:49.000'
insert into tb select 3139 ,'00241', '苏5', '北京公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:36:22.000'
insert into tb select 3140 ,'00257', '李6', '广州公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:37:30.000'
insert into tb select 3141 ,'00025', '李7', '深圳公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:39:07.000'
go
select 分公司,部门,
SUM(case when CONVERT(varchar(5),刷卡时间,108) between '08:00' and '10:00' then 1 else 0 end)早餐,
SUM(case when CONVERT(varchar(5),刷卡时间,108) between '12:00' and '13:00' then 1 else 0 end)午餐,
SUM(case when CONVERT(varchar(5),刷卡时间,108) between '18:00' and '19:00' then 1 else 0 end)晚餐,
count(*) 合计
from tb
group by 分公司,部门
--这个公司的人都在规定时间外用餐!
/*
分公司 部门 早餐 午餐 晚餐 合计
-------------------- -------------------- ----------- ----------- ----------- -----------
广州公司 保安部 0 0 0 2
深圳公司 保安部 0 0 0 1
北京公司 客房部 0 0 0 2
深圳公司 客房部 0 0 0 1
广州公司 前厅部 0 0 0 1
(5 行受影响)
*/
go
drop table tb
select
分公司 ,部门,
count(case when datepart(hh,刷卡时间) between 8 and 10 then 1 end) as 早餐次数,
count(case when datepart(hh,刷卡时间) between 12 and 13 then 1 end) as 中餐次数,
count(case when datepart(hh,刷卡时间) between 18 and 19 then 1 end) as 晚餐次数,
count(1) as 合计
from
tb
group by
分公司 ,部门
select
分公司 ,部门,
sum(case when datepart(hh,刷卡时间) between 8 and 10 then 1 else 0 end) as 早餐次数,
sum(case when datepart(hh,刷卡时间) between 12 and 13 then 1 else 0 end) as 中餐次数,
sum(case when datepart(hh,刷卡时间) between 18 and 19 then 1 else 0 end) as 晚餐次数,
count(1) as 合计
from
tb
group by
分公司 ,部门
create table tb(id int,员工编号 varchar(10),姓名 varchar(10),分公司 varchar(20),部门 varchar(20) , 刷卡日期 datetime,刷卡时间 datetime)
insert into tb values(3135 ,'00068', '罗1', '广州公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:28:56.000')
insert into tb values(3136 ,'00188', '李2', '深圳公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:29:58.000')
insert into tb values(3137 ,'00305', '刘3', '广州公司', '前厅部', '2011-10-11 00:00:00.000', '1900-01-01 16:35:22.000')
insert into tb values(3138 ,'00260', '骆4', '北京公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:35:49.000')
insert into tb values(3139 ,'00241', '苏5', '北京公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:36:22.000')
insert into tb values(3140 ,'00257', '李6', '广州公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:37:30.000')
insert into tb values(3141 ,'00025', '李7', '深圳公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:39:07.000')
go
select 分公司 ,部门,
sum(case when convert(varchar(8),刷卡时间,120) between '08:00:00' and '09:00:00' then 1 else 0 end) [早餐次数],
sum(case when convert(varchar(8),刷卡时间,120) between '12:00:00' and '13:00:00' then 1 else 0 end) [中餐次数],
sum(case when convert(varchar(8),刷卡时间,120) between '18:00:00' and '19:00:00' then 1 else 0 end) [晚餐次数],
sum(case when convert(varchar(8),刷卡时间,120) not between '08:00:00' and '09:00:00' and
convert(varchar(8),刷卡时间,120) not between '12:00:00' and '13:00:00' and
convert(varchar(8),刷卡时间,120) not between '18:00:00' and '19:00:00'
then 1 else 0 end) [其他],
count(1) 合计
from tb
group by 分公司 ,部门
drop table tb
/*
分公司 部门 早餐次数 中餐次数 晚餐次数 其他 合计
-------------------- -------------------- ----------- ----------- ----------- ----------- -----------
广州公司 保安部 0 0 0 2 2
深圳公司 保安部 0 0 0 1 1
北京公司 客房部 0 0 0 2 2
深圳公司 客房部 0 0 0 1 1
广州公司 前厅部 0 0 0 1 1
(所影响的行数为 5 行)
*/
select 分公司 ,部门,cast(刷卡日期 as char(7)) as 年月
sum(case when datepart(hh,刷卡时间) between 8 and 10 then 1 else 0 end) [早餐次数],
sum(case when datepart(hh,刷卡时间) between 12 and 13 then 1 else 0 end) [中餐次数],
sum(case when datepart(hh,刷卡时间) between 18 and 19 then 1 else 0 end) [晚餐次数],
count(1) 合计
from tb
group by 分公司 ,部门,cast(刷卡日期 as char(7))
create table tb(id int,员工编号 varchar(10),姓名 varchar(10),分公司 varchar(20),部门 varchar(20) , 刷卡日期 datetime,刷卡时间 datetime)
insert into tb values(3135 ,'00068', '罗1', '广州公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:28:56.000')
insert into tb values(3136 ,'00188', '李2', '深圳公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:29:58.000')
insert into tb values(3137 ,'00305', '刘3', '广州公司', '前厅部', '2011-10-11 00:00:00.000', '1900-01-01 16:35:22.000')
insert into tb values(3138 ,'00260', '骆4', '北京公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:35:49.000')
insert into tb values(3139 ,'00241', '苏5', '北京公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:36:22.000')
insert into tb values(3140 ,'00257', '李6', '广州公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:37:30.000')
insert into tb values(3141 ,'00025', '李7', '深圳公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:39:07.000')
go
select 分公司 ,部门,
sum(case when datepart(hh,刷卡时间) between 8 and 10 then 1 else 0 end) [早餐次数],
sum(case when datepart(hh,刷卡时间) between 12 and 13 then 1 else 0 end) [中餐次数],
sum(case when datepart(hh,刷卡时间) between 18 and 19 then 1 else 0 end) [晚餐次数],
sum(case when datepart(hh,刷卡时间) not between 8 and 10 and datepart(hh,刷卡时间) not between 12 and 13 and datepart(hh,刷卡时间) not between 18 and 19 then 1 else 0 end) [其他],
count(1) 合计
from tb
group by 分公司 ,部门
drop table tb
/*
分公司 部门 早餐次数 中餐次数 晚餐次数 其他 合计
-------------------- -------------------- ----------- ----------- ----------- ----------- -----------
广州公司 保安部 0 0 0 2 2
深圳公司 保安部 0 0 0 1 1
北京公司 客房部 0 0 0 2 2
深圳公司 客房部 0 0 0 1 1
广州公司 前厅部 0 0 0 1 1
(所影响的行数为 5 行)
*/
create table tb(id int,员工编号 varchar(10),姓名 varchar(10),分公司 varchar(20),部门 varchar(20) , 刷卡日期 datetime,刷卡时间 datetime)
insert into tb values(3135 ,'00068', '罗1', '广州公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:28:56.000')
insert into tb values(3136 ,'00188', '李2', '深圳公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:29:58.000')
insert into tb values(3137 ,'00305', '刘3', '广州公司', '前厅部', '2011-10-11 00:00:00.000', '1900-01-01 16:35:22.000')
insert into tb values(3138 ,'00260', '骆4', '北京公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:35:49.000')
insert into tb values(3139 ,'00241', '苏5', '北京公司', '客房部', '2011-10-11 00:00:00.000', '1900-01-01 16:36:22.000')
insert into tb values(3140 ,'00257', '李6', '广州公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:37:30.000')
insert into tb values(3141 ,'00025', '李7', '深圳公司', '保安部', '2011-10-11 00:00:00.000', '1900-01-01 16:39:07.000')
go
select 分公司 ,部门,
sum(case when datepart(hh,刷卡时间) between 8 and 10 then 1 else 0 end) [早餐次数],
sum(case when datepart(hh,刷卡时间) between 12 and 13 then 1 else 0 end) [中餐次数],
sum(case when datepart(hh,刷卡时间) between 18 and 19 then 1 else 0 end) [晚餐次数],
count(1) 合计
from tb
group by 分公司 ,部门
drop table tb
/*
分公司 部门 早餐次数 中餐次数 晚餐次数 合计
-------------------- -------------------- ----------- ----------- ----------- -----------
广州公司 保安部 0 0 0 2
深圳公司 保安部 0 0 0 1
北京公司 客房部 0 0 0 2
深圳公司 客房部 0 0 0 1
广州公司 前厅部 0 0 0 1
(所影响的行数为 5 行)
*/
select 分公司 ,部门,
sum(case when datepart(hh,刷卡时间) between 8 and 10 then 1 else 0 end) [早餐次数],
sum(case when datepart(hh,刷卡时间) between 12 and 13 then 1 else 0 end) [中餐次数],
sum(case when datepart(hh,刷卡时间) between 18 and 19 then 1 else 0 end) [晚餐次数],
count(1) 合计
from tb
group by 分公司 ,部门