Skip to content

Commit 68fd069

Browse files
committed
fix: fixed sum of all account at once in sum_by_category
1 parent a1342d9 commit 68fd069

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
CREATE OR REPLACE FUNCTION sum_by_category(start_date DATE, end_date DATE)
2-
RETURNS TABLE(category_id bigserial, total_amount DOUBLE PRECISION)
2+
RETURNS TABLE(account_id bigint, category_id bigint, total_amount DOUBLE PRECISION)
33
AS $$
44
BEGIN
55
RETURN QUERY
6-
SELECT category.id, SUM(transaction.amount) as total_amount
7-
FROM transaction
8-
JOIN category ON transaction.category = category.id
9-
WHERE transaction.created_at BETWEEN start_date AND end_date
10-
GROUP BY category.id
11-
ORDER BY total_amount DESC;
6+
SELECT t.id_account, c.id AS category_id, SUM(t.amount) AS total_amount
7+
FROM transaction t
8+
JOIN category c ON t.category = c.id
9+
WHERE t.created_at BETWEEN start_date AND end_date
10+
GROUP BY t.id_account, c.id
11+
ORDER BY t.id_account, total_amount DESC;
1212
END;
1313
$$
1414
LANGUAGE plpgsql;

0 commit comments

Comments
 (0)