Use date_trunc instead of splitting date into year and month

This commit is contained in:
2021-12-08 15:15:35 +00:00
parent c3a022b595
commit 7cb7527704
5 changed files with 36 additions and 45 deletions

View File

@ -124,37 +124,32 @@ func (q *Queries) GetCategories(ctx context.Context, budgetID uuid.UUID) ([]GetC
const getCategoriesWithBalance = `-- name: GetCategoriesWithBalance :many
SELECT categories.id, categories.name, category_groups.name as group,
(COALESCE(
(
SELECT SUM(a_hist.amount)
FROM assignments a_hist
WHERE categories.id = a_hist.category_id
AND a_hist.date < $1
)
, 0)+COALESCE(
(
SELECT SUM(t_hist.amount)
FROM transactions t_hist
WHERE categories.id = t_hist.category_id
AND t_hist.date < $1
)
, 0))::decimal(12,2) as available,
COALESCE(
(
SELECT SUM(t_this.amount)
FROM transactions t_this
WHERE categories.id = t_this.category_id
AND t_this.date >= $2 AND t_this.date < $1
)
, 0)::decimal(12,2) as activity,
COALESCE(
(
SELECT SUM(a_hist.amount)
FROM assignments a_hist
WHERE categories.id = a_hist.category_id
AND a_hist.date >= $2 AND a_hist.date < $1
)
,0)::decimal(12,2) as assigned
(
COALESCE((
SELECT SUM(a_hist.amount) FROM assignments a_hist
WHERE categories.id = a_hist.category_id AND a_hist.date < $1
), 0)+COALESCE((
SELECT SUM(t_hist.amount) FROM transactions t_hist
WHERE categories.id = t_hist.category_id AND t_hist.date < $1
), 0)
)::decimal(12,2) as available,
(
COALESCE((
SELECT SUM(a_hist.amount) FROM assignments a_hist
WHERE categories.id = a_hist.category_id AND a_hist.date < $2
), 0)+COALESCE((
SELECT SUM(t_hist.amount) FROM transactions t_hist
WHERE categories.id = t_hist.category_id AND t_hist.date < $2
), 0)
)::decimal(12,2) as available_last_month,
COALESCE((
SELECT SUM(t_this.amount) FROM transactions t_this
WHERE categories.id = t_this.category_id AND t_this.date >= $2 AND t_this.date < $1
), 0)::decimal(12,2) as activity,
COALESCE((
SELECT SUM(a_hist.amount) FROM assignments a_hist
WHERE categories.id = a_hist.category_id AND a_hist.date >= $2 AND a_hist.date < $1
), 0)::decimal(12,2) as assigned
FROM categories
INNER JOIN category_groups ON categories.category_group_id = category_groups.id