统计数据求救,各位帮忙啦。多谢

dizhisha 2010-11-04 04:08:39
部门	编号	姓名	日期		一次     二次     三次
办公室 0021 小妹 20101008.0 1229.0 NULL NULL
公司四部 0100 刘明 20101008.0 808.0 1235.0 1803.0
公司四部 0100 刘明 20101009.0 809.0 1156.0 1823.0
办公室 0021 小妹 20101011.0 817.0 1221.0 NULL
公司四部 0100 刘明 20101011.0 807.0 1151.0 1745.0
办公室 0021 小妹 20101012.0 821.0 1830.0 NULL
公司四部 0100 刘明 20101012.0 805.0 1157.0 NULL
办公室 0021 小妹 20101013.0 813.0 1224.0 NULL
公司四部 0100 刘明 20101013.0 807.0 1154.0 1753.0
公司四部 0100 刘明 20101014.0 813.0 1745.0 NULL
公司四部 0100 刘明 20101015.0 810.0 1157.0 1803.0
公司四部 0100 刘明 20101018.0 806.0 1203.0 1838.0
公司四部 0100 刘明 20101019.0 818.0 1241.0 1741.0


我的需求如下:
统计每位员工的一段时间就餐情况 ,早餐几次,中餐几次,晚餐几次
630--900为早餐 1125--1300为午餐 1500--2000为晚餐

字段【一次】、【二次】、【三次】为打卡时间 一天最多三次

...全文
86 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
abuying 2010-11-05
  • 打赏
  • 举报
回复
--630--900为早餐 1125--1300为午餐 1500--2000为晚餐
select 姓名,sum(早餐) 早餐,sum(午餐) 午餐,sum(晚餐)晚餐 from
(
select 姓名,
早餐=case when 一次 is null then 0 when 一次 between 630.0 and 900.0 then 1 else 0 end,
午餐=case when 二次 is null then 0 when 三次 between 1125.0 and 1300.0 then 1 else 0 end,
晚餐=case when 二次 is null then 0 when 三次 between 1500.0 and 2000.0 then 1 else 0 end
from tb
) t group by 姓名
dawugui 2010-11-04
  • 打赏
  • 举报
回复
create table tb(部门 varchar(10), 编号 varchar(10), 姓名 varchar(10), 日期 datetime, 一次 int, 二次 int, 三次 int)
insert into tb values('办公室' , '0021', '小妹', '20101008', 1229.0 , NULL , NULL)
insert into tb values('公司四部', '0100', '刘明', '20101008', 808.0 , 1235.0 , 1803.0)
insert into tb values('公司四部', '0100', '刘明', '20101009', 809.0 , 1156.0 , 1823.0)
insert into tb values('办公室' , '0021', '小妹', '20101011', 817.0 , 1221.0 , NULL)
insert into tb values('公司四部', '0100', '刘明', '20101011', 807.0 , 1151.0 , 1745.0)
insert into tb values('办公室' , '0021', '小妹', '20101012', 821.0 , 1830.0 , NULL)
insert into tb values('公司四部', '0100', '刘明', '20101012', 805.0 , 1157.0 , NULL)
insert into tb values('办公室' , '0021', '小妹', '20101013', 813.0 , 1224.0 , NULL)
insert into tb values('公司四部', '0100', '刘明', '20101013', 807.0 , 1154.0 , 1753.0)
insert into tb values('公司四部', '0100', '刘明', '20101014', 813.0 , 1745.0 , NULL)
insert into tb values('公司四部', '0100', '刘明', '20101015', 810.0 , 1157.0 , 1803.0)
insert into tb values('公司四部', '0100', '刘明', '20101018', 806.0 , 1203.0 , 1838.0)
insert into tb values('公司四部', '0100', '刘明', '20101019', 818.0 , 1241.0 , 1741.0)
go

--查所有日期。
select 姓名 ,
sum(case when shijian between 630 and 900 then 1 else 0 end) 早餐,
sum(case when shijian between 1125 and 1300 then 1 else 0 end) 中餐,
sum(case when shijian between 1500 and 2000 then 1 else 0 end) 晚餐
from
(
select 姓名 , 一次 shijian from tb where 一次 is not null
union all
select 姓名 , 二次 shijian from tb where 二次 is not null
union all
select 姓名 , 三次 shijian from tb where 三次 is not null
) t
group by 姓名
/*
姓名 早餐 中餐 晚餐
---------- ----------- ----------- -----------
刘明 9 8 8
小妹 3 3 1

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


--查20101008 - 20101015
select 姓名 ,
sum(case when shijian between 630 and 900 then 1 else 0 end) 早餐,
sum(case when shijian between 1125 and 1300 then 1 else 0 end) 中餐,
sum(case when shijian between 1500 and 2000 then 1 else 0 end) 晚餐
from
(
select 姓名 , 一次 shijian from tb where 一次 is not null and 日期 between '20101008' and '20101015'
union all
select 姓名 , 二次 shijian from tb where 二次 is not null and 日期 between '20101008' and '20101015'
union all
select 姓名 , 三次 shijian from tb where 三次 is not null and 日期 between '20101008' and '20101015'
) t
group by 姓名
/*
姓名 早餐 中餐 晚餐
---------- ----------- ----------- -----------
刘明 7 6 6
小妹 3 3 1

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

drop table tb
obuntu 2010-11-04
  • 打赏
  • 举报
回复
占座,学习。
jamk 2010-11-04
  • 打赏
  • 举报
回复
顶楼上的~
dawugui 2010-11-04
  • 打赏
  • 举报
回复

select 姓名 ,
sum(case when shijian between 630 and 900 then 1 else 0 end) 早餐,
sum(case when shijian between 1125 and 1300 then 1 else 0 end) 中餐,
sum(case when shijian between 1500 and 2000 then 1 else 0 end) 晚餐
from
(
select 姓名 , 一次 shijian from tb where 日期 between 日期1 and 日期2
union all
select 姓名 , 二次 shijian from tb where 日期 between 日期1 and 日期2
union all
select 姓名 , 三次 shijian from tb where 日期 between 日期1 and 日期2
) t
group by 姓名

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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