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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
set @dte = cast('2023-08-05' as date);
set @capacity = 6000;
set @open_hour = 8;
set @close_hour = 18;
with hours (hour)
as
(
select 0
union all
select 1
union all
select 2
union all
select 3
union all
select 4
union all
select 5
union all
select 6
union all
select 7
union all
select 8
union all
select 9
union all
select 10
union all
select 11
union all
select 12
union all
select 13
union all
select 14
union all
select 15
union all
select 16
union all
select 17
union all
select 18
union all
select 19
union all
select 20
union all
select 21
union all
select 22
union all
select 23
),
t_counter_in (cin_id, cin_counter_value, cin_counter_total, cin_time, cin_date)
as
(
select 40, 10, 340, cast('14:01:10' as time), cast('2023-08-05' as date)
union all
select 41, 10, 350, cast('14:05:39' as time), cast('2023-08-05' as date)
union all
select 42, 10, 360, cast('14:06:47' as time), cast('2023-08-05' as date)
union all
select 43, 10, 370, cast('14:11:14' as time), cast('2023-08-05' as date)
),
t_counter_out (cou_id, cou_counter_value, cou_counter_total, cou_time, cou_date)
as
(
select 27, 10, 340, cast('14:04:45' as time), cast('2023-08-05' as date)
union all
select 28, 10, 350, cast('14:08:18' as time), cast('2023-08-05' as date)
union all
select 29, 10, 360, cast('14:24:58' as time), cast('2023-08-05' as date)
union all
select 30, 10, 370, cast('14:34:59' as time), cast('2023-08-05' as date)
),
t_counter (cou_date, cou_time, cou_value)
as
(
select cin_date, cin_time, cin_counter_value
from t_counter_in
union all
select cou_date, cou_time, -cou_counter_value
from t_counter_out
),
sum_cumul (cou_date, cou_time, nb_present)
as
(
select cou_date, cou_time, sum(cou_value) over (partition by cou_date, hour(cou_time) order by cou_date, cou_time)
from t_counter
),
sum_present (day, hour, nb_present)
as
(
select cou_date, hour(cou_time), max(nb_present)
from sum_cumul
group by cou_date, hour(cou_time)
),
sum_in (day, hour, nb_in, total_in)
as
(
select cin_date, hour(cin_time), sum(cin_counter_value), max(cin_counter_total)
from t_counter_in
group by cin_date, hour(cin_time)
),
sum_out (day, hour, nb_out, total_out)
as
(
select cou_date, hour(cou_time), sum(cou_counter_value), max(cou_counter_total)
from t_counter_out
group by cou_date, hour(cou_time)
)
select h.hour, i.nb_in entrants, o.nb_out, p.nb_present, @capacity - p.nb_present
from hours h
left outer join sum_in i on i.day = @dte and i.hour = h.hour
left outer join sum_out o on o.day = @dte and o.hour = h.hour
left outer join sum_present p on p.day = @dte and o.hour = h.hour
where h.hour between @open_hour and @close_hour; |
Partager