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

Posted

技术标签:

【中文标题】在一天的开始和结束时选择并计算条目数【英文标题】:select and count number of entries at the start and end of day 【发布时间】:2017-11-16 01:43:41 【问题描述】:

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

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 

【问题讨论】:

你尝试过什么来达到你的结果。并且还不清楚在什么条件下确定商店开店和关店时员工在店内。 考虑一名员工在2017-04-24 08:42:00.000 打卡,而他在2017-04-24 22:00:00.000 或之前没有punch Out。在这种情况下,员工是否应计入No. Of Emp At Close 您正在使用哪个DBMSmysqlSql Server 他们怎么能同时出拳? 【参考方案1】:

您可以使用以下查询来查找员工是否仍在办公室。然后,您可以从该查询的输出中计算出在办公室和不在办公室的员工人数。

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"

【讨论】:

【参考方案2】:
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]

【讨论】:

【参考方案3】:

你可以使用这个:

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

【讨论】:

【参考方案4】:

试试这个

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'))

【讨论】:

【参考方案5】:

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

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

【讨论】:

以上是关于在一天的开始和结束时选择并计算条目数的主要内容,如果未能解决你的问题,请参考以下文章

如何在一周中的每一天的每一天中创建空条目

如何在不使用 NSCalendar 的情况下创建 NSDates(一天的开始和结束)?

启用夏令时时如何在 T-SQL 中获取一天的开始和结束?

在 Python 上的 Plotly 中手动定义图例

给定一个 Unix 时间戳,如何获得那一天的开始和结束?

在给定开始和结束时间的情况下计算并发会话