Use date_trunc instead of splitting date into year and month
This commit is contained in:
parent
c3a022b595
commit
7cb7527704
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user