diff --git a/postgres/assignments.sql.go b/postgres/assignments.sql.go index 9ef2fc6..5398652 100644 --- a/postgres/assignments.sql.go +++ b/postgres/assignments.sql.go @@ -54,7 +54,7 @@ func (q *Queries) DeleteAllAssignments(ctx context.Context, budgetID uuid.UUID) } const getAssignmentsByMonthAndCategory = `-- name: GetAssignmentsByMonthAndCategory :many -SELECT year, month, category_id, budget_id, amount +SELECT date, category_id, budget_id, amount FROM assignments_by_month WHERE assignments_by_month.budget_id = $1 ` @@ -69,8 +69,7 @@ func (q *Queries) GetAssignmentsByMonthAndCategory(ctx context.Context, budgetID for rows.Next() { var i AssignmentsByMonth if err := rows.Scan( - &i.Year, - &i.Month, + &i.Date, &i.CategoryID, &i.BudgetID, &i.Amount, diff --git a/postgres/categories.sql.go b/postgres/categories.sql.go index 2acf888..6d1d14c 100644 --- a/postgres/categories.sql.go +++ b/postgres/categories.sql.go @@ -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 diff --git a/postgres/models.go b/postgres/models.go index 22139ec..ed57572 100644 --- a/postgres/models.go +++ b/postgres/models.go @@ -25,8 +25,7 @@ type Assignment struct { } type AssignmentsByMonth struct { - Year float64 - Month float64 + Date time.Time CategoryID uuid.UUID BudgetID uuid.UUID Amount int64 @@ -68,8 +67,7 @@ type Transaction struct { } type TransactionsByMonth struct { - Year float64 - Month float64 + Date time.Time CategoryID uuid.NullUUID BudgetID uuid.UUID Amount int64 diff --git a/postgres/schema/202112081529_views-for-months.sql b/postgres/schema/202112081529_views-for-months.sql index d6c5eaf..c171dc9 100644 --- a/postgres/schema/202112081529_views-for-months.sql +++ b/postgres/schema/202112081529_views-for-months.sql @@ -1,16 +1,16 @@ -- +goose Up CREATE VIEW transactions_by_month AS - SELECT extract(year from transactions.date) as year, extract(month from transactions.date) as month, transactions.category_id, accounts.budget_id, SUM(amount) as amount + SELECT date_trunc('month', transactions.date)::date as date, transactions.category_id, accounts.budget_id, SUM(amount) as amount FROM transactions INNER JOIN accounts ON accounts.id = transactions.account_id - GROUP BY extract(year from transactions.date), extract(month from transactions.date), transactions.category_id, accounts.budget_id; + GROUP BY date_trunc('month', transactions.date), transactions.category_id, accounts.budget_id; CREATE VIEW assignments_by_month AS - SELECT extract(year from assignments.date) as year, extract(month from assignments.date) as month, assignments.category_id, category_groups.budget_id, SUM(amount) as amount + SELECT date_trunc('month', assignments.date)::date as date, assignments.category_id, category_groups.budget_id, SUM(amount) as amount FROM assignments INNER JOIN categories ON categories.id = assignments.category_id INNER JOIN category_groups ON categories.category_group_id = category_groups.id - GROUP BY extract(year from assignments.date), extract(month from assignments.date), assignments.category_id, category_groups.budget_id; + GROUP BY date_trunc('month', assignments.date), assignments.category_id, category_groups.budget_id; -- +goose Down DROP VIEW transactions_by_month; diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index 4b427c4..a79cdbf 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -64,7 +64,7 @@ func (q *Queries) DeleteAllTransactions(ctx context.Context, budgetID uuid.UUID) } const getTransactionsByMonthAndCategory = `-- name: GetTransactionsByMonthAndCategory :many -SELECT year, month, category_id, budget_id, amount +SELECT date, category_id, budget_id, amount FROM transactions_by_month WHERE transactions_by_month.budget_id = $1 ` @@ -79,8 +79,7 @@ func (q *Queries) GetTransactionsByMonthAndCategory(ctx context.Context, budgetI for rows.Next() { var i TransactionsByMonth if err := rows.Scan( - &i.Year, - &i.Month, + &i.Date, &i.CategoryID, &i.BudgetID, &i.Amount,