34,587
社区成员
发帖
与我相关
我的任务
分享
部门 编号 姓名 日期 一次 二次 三次
办公室 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为晚餐
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 姓名
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
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 姓名