选择并计算一天开始和结束时的条目数

时间:2017-06-14 04:54:46

标签: mysql sql sql-server sql-server-2008

基本上我想知道,当商店开放和关闭时,有多少员工,详情如下

EmployeeID | IN and OUT Time         | Type       | InOut ID | Comments    |
---------- | ----------------------- |------------| ---------|-------------|
12961        2017-04-24 08:07:00.000    Punch In      1        Store Open
12680        2017-04-24 08:07:00.000    Punch In      2
12662        2017-04-24 08:07:00.000    Punch In      3
12683        2017-04-24 08:27:00.000    Punch In      4        
12864        2017-04-24 08:42:00.000    Punch In      5
12681        2017-04-24 10:03:00.000    Punch In      6
-1           2017-04-24 13:33:00.000    Punch In      7
12662        2017-04-24 18:00:00.000    Punch Out     8
12683        2017-04-24 18:00:00.000    Punch Out     9
12864        2017-04-24 18:35:00.000    Punch Out     10 
12681        2017-04-24 22:00:00.000    Punch Out     11
12960        2017-04-24 22:00:00.000    Punch Out     12 
12959        2017-04-24 22:00:00.000    Punch Out     13 
-1           2017-04-24 22:00:00.000    Punch Out     14        Store Close

结果:

Header               | Header
-------------------- | ------
No. Of Emp At Open   | 3 
No. Of Emp At Close  | 4 

5 个答案:

答案 0 :(得分:0)

您可以使用以下查询来查找员工是否仍在办公室。然后,您可以根据此查询的输出计算办公室和办公室外的员工编号。

SELECT in.EmployeeID , 
       CASE WHEN out.EmployeeID IS NULL 
             THEN 1
            ELSE 0
       END AS Still_In_Office                   
FROM Employee in
LEFT OUTER JOIN Employee out ON in.EmployeeID  = out.EmployeeID 
                                AND out.Type = "Punch Out"
WHERE in.Type = "Punch In"

答案 1 :(得分:0)

select 'No. Of Emp At Open',count([IN and OUT Time]) from Employee where [IN and OUT Time] in (select [IN and OUT Time] from Employee where Comments= 'Store Open') group by [IN and OUT Time]union all select 'No. Of Emp At Close', count([IN and OUT Time]) from Employee where [IN and OUT Time] in (select [IN and OUT Time] from Employee where Comments= 'Store Close') group by [IN and OUT Time]

答案 2 :(得分:0)

您可以使用此功能:

SELECT
T.IN_OUT_TYPE AS Header1
,(SELECT COUNT(*) FROM tableABC ABC WHERE ABC.IN_and_OUT_ Time = T.IN_and_OUT_ Time) AS Header2
FROM
(
SELECT 
'No. Of Emp At Open' AS IN_OUT_TYPE
IN_and_OUT_ Time
FROM tableABC
WHERE Comments = 'Store Open'

UNION ALL

SELECT 
'No. Of Emp At Close' AS IN_OUT_TYPE
IN_and_OUT_ Time
FROM tableABC
WHERE Comments = 'Store Close'
) AS T

答案 3 :(得分:0)

试试这个

select [IN and OUT Time],count(*) from table 
where  [IN and OUT Time] in(select [IN and OUT Time] from table 
where Comments  in('Store Open','Store Close'))

答案 4 :(得分:0)

没有正确解释。 什么是正确的银泰,包括恩典?

declare @CorrectInTimeIncludingGrace datetime='8:15'
declare @CorrectOutTimeIncludingGrace datetime='22:00'
declare @AttendanceDate datetime='2017-04-24'
declare @AttendanceInDate datetime
declare @AttendanceOutDate datetime
set @AttendanceInDate=dateadd(minute,datepart(minute,@CorrectInTimeIncludingGrace)
, dateadd(hour,datepart(hour,@CorrectInTimeIncludingGrace), @AttendanceDate))

set @AttendanceOutDate=dateadd(minute,datepart(minute,@CorrectOutTimeIncludingGrace)
, dateadd(hour,datepart(hour,@CorrectOutTimeIncludingGrace), @AttendanceDate))

declare @t table(EmployeeID int,IN_OUT dateTime, Type varchar(20),InOutID int, Comments varchar(40))
insert into @t  VALUES 
(12961,'2017-04-24 08:07:00.000', 'Punch In' ,  1 ,'Store Open')
,(12680,'2017-04-24 08:07:00.000','Punch In', 2 ,  null)
,(12662,'2017-04-24 08:07:00.000','Punch In', 3 ,  null)
,(12683,'2017-04-24 08:27:00.000','Punch In', 4  ,  null)
,(12864,'2017-04-24 08:42:00.000','Punch In', 5 ,  null)
,(12681,'2017-04-24 10:03:00.000','Punch In', 6 ,  null)
,(-1   ,'2017-04-24 13:33:00.000','Punch In', 7 ,  null)
,(12662,'2017-04-24 18:00:00.000','Punch Out', 8    ,  null)
,(12683,'2017-04-24 18:00:00.000','Punch Out', 9    ,  null)
,(12864,'2017-04-24 18:35:00.000','Punch Out', 10,  null)
,(12681,'2017-04-24 22:00:00.000','Punch Out', 11,  null)
,(12960,'2017-04-24 22:00:00.000','Punch Out', 12,  null)
,(12959,'2017-04-24 22:00:00.000','Punch Out', 13,  null)
,(-1   ,'2017-04-24 22:00:00.000','Punch Out', 14, 'Store Close')

select 'No. Of Emp At Open' as Header, count(*)HeaderCount
from @t where
IN_OUT<= @AttendanceInDate
union ALL
select 'No. Of Emp At Close' , count(*)
from @t where
IN_OUT>= @AttendanceOutDate