-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalls.sql
49 lines (36 loc) · 1.06 KB
/
calls.sql
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
-- parametry wejsciowe
declare @starttime time = '07:00:00'
declare @stoptime time = '18:00:00'
declare @delta int = 20
declare @date datetime = '2019-05-31'
-- zmienne robocze
declare @q int = DATEDIFF(second, 0, @starttime) / @delta
declare @datetime datetime = @date + cast(@starttime as datetime)
-- select DATEDIFF(s, @starttime, @stoptime) / @delta
declare @start int = 1
declare @end int = DATEDIFF(s, @starttime, @stoptime) / @delta
;with numcte
AS
(
SELECT @start slot
UNION all
SELECT slot + 1 FROM numcte WHERE slot < @end
),
mycalls as (SELECT
c.LineId,
c.StartTime,
DATEDIFF(SECOND, CONVERT(DATE,@datetime), c.StartTime) / @delta - @q as slotstart,
c.StopTime,
DATEDIFF(SECOND, CONVERT(DATE,@datetime), c.StopTime) / @delta - @q as slotstop
from
Calls as c ),
lines as (select Distinct LineId from Calls)
SELECT slot,
lineid,
(select count(*) from mycalls as c
where slot >= c.slotstart
and slot <= c.slotstop
and c.LineId = lines.LineId
) as quantity
FROM numcte, lines option (maxrecursion 0)
-- select * from vwCalls