每小时计算员工数

时间:2018-01-28 02:38:40

标签: sql-server

我在mssql中有一个表格如下:

~/u/tmp/JNK> ls -latTr
total 32
drwxr-xr-x  160 BNW  staff  5440 Jan 27 18:47:37 2018 ../
-rw-r--r--    1 BNW  staff     0 Jan 27 19:30:10 2018 Henry-James.txt
-rw-r--r--    1 BNW  staff     0 Jan 27 19:30:17 2018 Emily-Dickinson.txt
-rw-r--r--    1 BNW  staff     0 Jan 27 19:30:21 2018 for-Henry-James.txt
-rwxr-xr-x    1 BNW  staff    97 Jan 27 19:31:55 2018 myscript*
-rw-r--r--@   1 BNW  staff  6148 Jan 27 19:43:15 2018 .DS_Store
drwxr-xr-x    2 BNW  staff    68 Jan 27 19:44:51 2018 DIRECTORY/
lrwxr-xr-x    1 BNW  staff    19 Jan 27 19:45:09 2018 softlink@ -> for-Henry-James.txt
drwxr-xr-x    9 BNW  staff   306 Jan 27 19:45:09 2018 ./

~/u/tmp/JNK> find . -type f
./.DS_Store
./Emily-Dickinson.txt
./for-Henry-James.txt
./Henry-James.txt
./myscript

~/u/tmp/JNK> find . -type f -name "*James*"
./for-Henry-James.txt
./Henry-James.txt

~/u/tmp/JNK> cat myscript 

#!/bin/bash
for arg in $@
do
   printf '%s' "arg="
   printf '%s' "$arg"
   printf '\n'
done
find . $@


~/u/tmp/JNK> ./myscript -type f
arg=-type
arg=f
./.DS_Store
./Emily-Dickinson.txt
./for-Henry-James.txt
./Henry-James.txt
./myscript

~/u/tmp/JNK> ./myscript -type f -name "*James*"
arg=-type
arg=f
arg=-name
arg=Henry-James.txt
arg=for-Henry-James.txt
find: for-Henry-James.txt: unknown primary or operator

~/u/tmp/JNK> ./myscript -type f -name '*James*'
arg=-type
arg=f
arg=-name
arg=Henry-James.txt
arg=for-Henry-James.txt
find: for-Henry-James.txt: unknown primary or operator
~/u/tmp/JNK> 

我想要每小时的员工人数:

Operator    Time                    Event
DG01E0020   2018-01-27 09:36:46.000 12
DG01E0020   2018-01-27 15:25:53.000 13 
DG01E0027   2018-01-27 10:54:53.000 12
DG01E0027   2018-01-27 19:39:13.000 13
DG01E0025   2018-01-27 11:48:44.000 12
DG01E0025   2018-01-27 15:32:02.000 13
DG01E0013   2018-01-27 12:02:56.000 12
DG01E0013   2018-01-27 17:50:41.000 13

如果可以帮助我查询:

Time.    Count
8:00     0
9:00     1
10:00    2
11:00    3
12:00    8

Etc.. 

现在我需要每小时了解正在工作的员工人数。

此表中的查询结果为:

declare @ontem datetime
declare @hoje datetime
set @ontem = CAST(CONVERT(VARCHAR(10), GETDATE() - 1, 101) + ' 06:00:00'  AS DATETIME)
set @hoje = CAST(CONVERT(VARCHAR(10), GETDATE(), 101) + ' 06:00:00' AS DATETIME)
SELECT
  a1.Operator, a1.Time as clockIN, (SELECT top 1 a2.Time FROM dg01.dbo.wgclogfilepos a2
   where a1.Operator = a2.Operator 
      and a2.Event = 13 
      and a1.Time >= @ontem 
      and a1.Time <= @hoje 
      and a2.Time > a1.Time 
   order by a2.Time) AS clockout
FROM dg01.dbo.wgclogfilepos a1 
WHERE Event = 12 
   and a1.Time >= @ontem 
   and a1.Time <= @hoje
ORDER BY a1.Time

0 个答案:

没有答案