0

员工在家或办公室开始新任务时输入下表

[tablename=CHECK]
c_id   c_sdate                c_emp       c_task
-------------------------------------------------
1     2013-05-01 01:01:00       1           26    //date 01 from home-----
2     2013-05-01 08:11:00       1           27    //date 01 from office--- Present

3     2013-05-02 03:41:00       1           28    //date 02 from home---
4     2013-05-02 09:12:00       1           29    //date 02 from office-
5     2013-05-02 22:32:00       1           30    //date 02 from home---Present

6     2013-05-03 01:43:00       1           31    //date 03 from home
7     2013-06-03 23:25:00       1           32    //date 03 from home----------Homework

8     2013-06-03 02:15:00       2           33    //other employee

如果在上午 8 点到晚上 8 点之间有 1 条或多条记录,则该员工将被视为在场

如果有 1 条或多条记录的时间不在上午 8 点到晚上 8 点之间,并且当天没有出现,
该员工将被视为已工作只有那天他没有怨恨才算)

我想显示员工的月度报告,例如。c_emp=1 表示月份,例如。1 个查询中有 5 个这样的

c_emp  presentCount   HW_Count
  1       3             1   

或单独查询 1

c_emp  presentCount  
  1       3

和查询 2

c_emp   HW_Count
  1        1     

我试过计算现在的工作正常

select count(distinct(date_format(c_sdate,'%e'))) as count 
from ita_check
where date_format(c_sdate,'%m')=5 
and c_emp=1 
and date_format(c_sdate,'%H%i')>=800
and date_format(c_sdate,'%H%i')<=2000

并为从家计数给出错误计数

select count(distinct(date_format(c_sdate,'%e'))) as count 
from ita_check
where date_format(c_sdate,'%m')=5
and c_eid=1
and c_id not in (
   select c_id 
   from ita_check
   where date_format(c_sdate,'%m')=5 
   and c_eid=1
   and (date_format(c_sdate,'%H%i')<=800 or date_format(c_sdate,'%H%i')>=2000)
)
and date_format(c_sdate,'%H%i')<800
or date_format(c_sdate,'%H%i')>2000

在上面的计数查询中,子查询返回 1 和 2,而外部消除 c_id=2 但不是 c_id=1

4

1 回答 1

4

试试这个查询

SELECT c_emp, 
sum(if(cnt>=1,1,0)) as Office, 
count(*)-sum(if(cnt>=1,1,0)) as WFH   from (
select c_emp, Date(c_sdate),
sum(if(c_sdate BETWEEN Date(c_sdate) + interval 8 hour 
       AND Date(c_sdate) + interval 20 hour, 1, 0)) as cnt
from table1
group by c_emp, Date(c_sdate)) tmp
group by c_emp

SQL 小提琴

| C_EMP | OFFICE | WFH |
------------------------
|     1 |      2 |   2 |
|     2 |      0 |   1 |

对于月度报告

SELECT c_emp, date_format(c_date, '%c %Y') as Mnth,
sum(if(cnt>=1,1,0)) as Office, 
count(*)-sum(if(cnt>=1,1,0)) as WFH   from (
select c_emp, Date(c_sdate) as c_date,
sum(if(c_sdate BETWEEN Date(c_sdate) + interval 8 hour 
       AND Date(c_sdate) + interval 20 hour, 1, 0)) as cnt
from table1
group by c_emp, Date(c_sdate)) tmp
group by c_emp,Mnth 

SQL 小提琴

| C_EMP |   MNTH | OFFICE | WFH |
---------------------------------
|     1 | 5 2013 |      2 |   1 |
|     1 | 6 2013 |      0 |   1 |
|     2 | 6 2013 |      0 |   1 |
于 2013-05-16T11:16:21.383 回答