-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc.sql
More file actions
25 lines (25 loc) · 1.05 KB
/
rpc.sql
File metadata and controls
25 lines (25 loc) · 1.05 KB
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
CREATE OR REPLACE FUNCTION get_practice_summary(p_start_date TIMESTAMP, p_end_date TIMESTAMP)
RETURNS TABLE (
month TIMESTAMPTZ,
year TIMESTAMPTZ,
minutes BIGINT
) AS $$
BEGIN
RETURN QUERY
SELECT
date_trunc('month'::text, practice_records.end_date) AS month,
date_trunc('year'::text, practice_records.end_date) AS year,
SUM(practice_records.minutes)::BIGINT AS minutes -- Converte para BIGINT
FROM practice_records
WHERE practice_records.end_date IS NOT NULL
AND practice_records.user_id = requesting_user_id() -- Assuming it returns text
AND practice_records.end_date >= p_start_date -- Filtro de data inicial
AND practice_records.end_date <= p_end_date -- Filtro de data final
GROUP BY
date_trunc('month'::text, practice_records.end_date),
date_trunc('year'::text, practice_records.end_date)
ORDER BY
date_trunc('year'::text, practice_records.end_date) ASC,
date_trunc('month'::text, practice_records.end_date) ASC;
END;
$$ LANGUAGE plpgsql;