2021年2月9日星期二

How to count records by store, day wise and in 2 hours range period with pivot table format?

I have multiple stores records with user's punch records. I would like to create a report for each store' day-wise which 2 hours have how many employees was working?

Clock In ID Last Name   First Name  In time Out time  912 Bedolla Jorge   1/1/2021 7:29   1/1/2021 11:31  912 Romero  Gabriel 1/1/2021 10:55  1/1/2021 14:07  912 Bedolla Jorge   1/1/2021 12:00  1/1/2021 16:07  912 Zaragoza    Daniel  1/1/2021 13:06  1/1/2021 14:57  912 Thaxton Christopher 1/1/2021 14:01  1/1/2021 16:57  912 Jones   Elena   1/1/2021 14:01  1/1/2021 16:35  912 Zaragoza    Daniel  1/1/2021 15:12  1/1/2021 17:09  912 Jones   Elena   1/1/2021 16:45  1/1/2021 18:05  912 Smith   Kirsten 1/1/2021 17:30  1/1/2021 20:01  912 Zaragoza    Daniel  1/1/2021 17:41  1/1/2021 21:49  

Looking for a result something like below. (below result data is incorrect)

store   ForDate 0-2 2-4 4-6 6-8 8-10 10-12 12-14 14-16 16-18 18-20 20-22 22-0  912 2021-01-01  0   0   0   1   0   1   2   3   3   2   3   0  912 2021-01-02  0   0   2   1   2   3   2   4   2   3   3   0  912 2021-01-03  0   0   1   1   2   2   2   2   3   0   2   0  912 2021-01-04  0   0   2   0   2   1   2   2   3   3   1   0  912 2021-01-05  0   0   2   1   1   3   4   4   2   2   1   0  912 2021-01-06  0   0   2   0   2   1   2   3   3   2   3   0  912 2021-01-07  0   0   2   1   2   1   3   4   2   2   0   0  912 2021-01-08  0   0   2   2   2   1   3   2   1   2   1   0  912 2021-01-09  0   0   1   1   0   3   1   3   2   2   3   0  912 2021-01-10  0   0   2   2   1   2   2   1   1   2   2   0  

I tried to solve with below query but it's wrong and stil it's just inTime but outTime is pending.

SELECT TOP 10 store, ForDate,   ISNULL([0], 0) + ISNULL([1], 0) AS [0-1],  ISNULL([2], 0) + ISNULL([3], 0) AS [2-3],  ISNULL([4], 0) + ISNULL([5], 0) AS [4-5],  ISNULL([6], 0) + ISNULL([7], 0) AS [6-7],  ISNULL([8], 0) + ISNULL([9], 0) AS [8-9],  ISNULL([10], 0) + ISNULL([11], 0) AS [10-11],  ISNULL([12], 0) + ISNULL([13], 0) AS [12-13],  ISNULL([14], 0) + ISNULL([15], 0) AS [14-15],  ISNULL([16], 0) + ISNULL([17], 0) AS [16-17],  ISNULL([18], 0) + ISNULL([19], 0) AS [18-19],  ISNULL([20], 0) + ISNULL([21], 0) AS [20-21],  ISNULL([22], 0) + ISNULL([23], 0) AS [22-23]  FROM (  select *  from   (    select store, CAST(InTime as date) AS ForDate, DATEPART(hour,InTime) AS OnHour, COUNT(*) AS Totals    from Punches    GROUP BY store, CAST(InTime as date),         DATEPART(hour,InTime)  ) src  pivot  (    sum(Totals)     for OnHour in  ([0],[1], [2], [3],[4], [5], [6],[7],[8], [9], [10],[11], [12], [13],[14], [15], [16],[17],[18], [19],[20],[21], [22], [23])   ) piv   ) t1  order by store, ForDate  

Here is SQL Fiddle with data.

https://www.db-fiddle.com/f/jo4atDmmj8cshyK1CWWo7x/2

https://stackoverflow.com/questions/66130772/how-to-count-records-by-store-day-wise-and-in-2-hours-range-period-with-pivot-t February 10, 2021 at 12:04PM

没有评论:

发表评论