-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
70 lines (66 loc) · 1.95 KB
/
queries.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
drop function if exists total_production;
CREATE OR REPLACE FUNCTION total_production(
machine_code varchar(50) default NULL,
shift_code varchar(50) default NULL,
target_date date default current_date,
prev_days integer default 7
)
-- Return type
returns table (
"date" text,
"machine" varchar(50),
"shift" varchar(50),
"start" time without time zone,
"end" time without time zone,
"total_produced" bigint
)
as
$body$
begin
/*
Function that evaluates the total production of parts, per day, machine and shift.
Params:
machine_code: a unique alphanumeric code, that uniquely identifies the machine
shift_code: a unique alphanumeric code, that uniquely identifies the shift
target_date: the current date
prev_days: number of past days in which the query will base itself to get the data
Usage:
select * from total_production(); -- Complete report
select * from total_production( -- For the PRESS machine, at the NIGHT shift, for the last 2 days
machine_code => 'press',
prev_days => 2,
shift_code => 'night'
);
*/
return query
select
to_char(timestamp_value::date, 'YYYY-MM-DD') as "date",
m.code as "machine",
s.code as "shift",
s.hour_start as "start",
s.hour_end as "end",
SUM("value") as "total_produced"
FROM "values" v
INNER JOIN machines m ON m.id = v.counter_id
INNER JOIN counters c ON c.id = m.counter_id
inner join shifts s on s.machine_id = m.id
-- Date range filter
where timestamp_value::date
between (target_date - interval '1 day' * prev_days) and target_date
-- Time range (shift) filter
and timestamp_value::time
between s.hour_start and s.hour_end
-- Conditional filtering for both machine and shift codes
and (machine_code IS NULL OR upper(m.code) = upper(machine_code))
and (shift_code IS NULL OR upper(s.code) = upper(shift_code))
GROUP BY (
"date",
"machine",
"shift",
"start",
"end"
)
order by "date" desc;
end;
$body$
language plpgsql volatile;