-
Notifications
You must be signed in to change notification settings - Fork 0
/
Magazyn_raportowanie czasu pracy
82 lines (82 loc) · 1.91 KB
/
Magazyn_raportowanie czasu pracy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
declare @poczatek DATETIME = '2024-08-09T04:00:00.000Z';
declare @koniec DATETIME = '2024-08-09T18:59:59.999Z';
WITH workline AS (
SELECT
wl.UserId,
wu.UserName,
wl.DataAreaId,
wl.partition,
CASE
WHEN WMSLOCATIONID LIKE '1M0%' THEN 'PARTER'
WHEN WMSLOCATIONID LIKE '1M1%' THEN '1 PIETRO'
WHEN WMSLOCATIONID LIKE '1M2%' THEN '2 PIETRO'
ELSE 'POZA MEZANIN' END AS 'LOC_TYPE',
wl.WorkClosedUTCDateTime as start_time,
wl.WorkClosedUTCDateTime as end_Time,
ROW_NUMBER() OVER (
PARTITION BY wl.USERID
ORDER BY wl.WorkClosedUTCDateTime
) row_num
FROM
WHSWORKLINE wl
join WHSWorkUser wu
on wl.UserId = wu.UserId and wl.DataAreaId = wu.DataAreaId and wl.PARTITION = wu.PARTITION
WHERE
wl.WMSLOCATIONID NOT IN ('LADM_OUT')
and WorkClosedUTCDateTime >= @poczatek
and WorkClosedUTCDateTime <= @koniec
and wl.userid = '3100292')
,SessionCTE as(
SELECT
UserId,
UserName,
DataAreaId,
partition,
LOC_TYPE,
start_time,
end_time,
row_num
FROM
workline
WHERE
row_num = 1
UNION ALL
SELECT
t.userid,
t.UserName,
t.DataAreaId,
t.partition,
t.LOC_TYPE,
CASE
WHEN t.LOC_TYPE = cte.LOC_TYPE THEN cte.start_time
ELSE t.end_time
END AS end_time,
t.start_time,
t.ROW_NUM
FROM
WORKLINE t
JOIN
SessionCTE cte
ON
t.userid = cte.userid AND
t.DataAreaId = cte.DataAreaId AND
t.partition = cte.partition AND
t.row_num = cte.row_num + 1
)
SELECT
userid,
LOC_TYPE,
DataAreaId,
start_time,
MAX(end_time) AS end_time
FROM
SessionCTE
GROUP BY
userid,
LOC_TYPE,
DataAreaId,
start_time
ORDER BY
userid,
start_time
OPTION (MAXRECURSION 0)