Use date_trunc instead of splitting date into year and month

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

View File

@ -54,7 +54,7 @@ func (q *Queries) DeleteAllAssignments(ctx context.Context, budgetID uuid.UUID)
} }
const getAssignmentsByMonthAndCategory = `-- name: GetAssignmentsByMonthAndCategory :many const getAssignmentsByMonthAndCategory = `-- name: GetAssignmentsByMonthAndCategory :many
SELECT year, month, category_id, budget_id, amount SELECT date, category_id, budget_id, amount
FROM assignments_by_month FROM assignments_by_month
WHERE assignments_by_month.budget_id = $1 WHERE assignments_by_month.budget_id = $1
` `
@ -69,8 +69,7 @@ func (q *Queries) GetAssignmentsByMonthAndCategory(ctx context.Context, budgetID
for rows.Next() { for rows.Next() {
var i AssignmentsByMonth var i AssignmentsByMonth
if err := rows.Scan( if err := rows.Scan(
&i.Year, &i.Date,
&i.Month,
&i.CategoryID, &i.CategoryID,
&i.BudgetID, &i.BudgetID,
&i.Amount, &i.Amount,

View File

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

View File

@ -25,8 +25,7 @@ type Assignment struct {
} }
type AssignmentsByMonth struct { type AssignmentsByMonth struct {
Year float64 Date time.Time
Month float64
CategoryID uuid.UUID CategoryID uuid.UUID
BudgetID uuid.UUID BudgetID uuid.UUID
Amount int64 Amount int64
@ -68,8 +67,7 @@ type Transaction struct {
} }
type TransactionsByMonth struct { type TransactionsByMonth struct {
Year float64 Date time.Time
Month float64
CategoryID uuid.NullUUID CategoryID uuid.NullUUID
BudgetID uuid.UUID BudgetID uuid.UUID
Amount int64 Amount int64

View File

@ -1,16 +1,16 @@
-- +goose Up -- +goose Up
CREATE VIEW transactions_by_month AS 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 FROM transactions
INNER JOIN accounts ON accounts.id = transactions.account_id 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 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 FROM assignments
INNER JOIN categories ON categories.id = assignments.category_id INNER JOIN categories ON categories.id = assignments.category_id
INNER JOIN category_groups ON categories.category_group_id = category_groups.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 -- +goose Down
DROP VIEW transactions_by_month; DROP VIEW transactions_by_month;

View File

@ -64,7 +64,7 @@ func (q *Queries) DeleteAllTransactions(ctx context.Context, budgetID uuid.UUID)
} }
const getTransactionsByMonthAndCategory = `-- name: GetTransactionsByMonthAndCategory :many const getTransactionsByMonthAndCategory = `-- name: GetTransactionsByMonthAndCategory :many
SELECT year, month, category_id, budget_id, amount SELECT date, category_id, budget_id, amount
FROM transactions_by_month FROM transactions_by_month
WHERE transactions_by_month.budget_id = $1 WHERE transactions_by_month.budget_id = $1
` `
@ -79,8 +79,7 @@ func (q *Queries) GetTransactionsByMonthAndCategory(ctx context.Context, budgetI
for rows.Next() { for rows.Next() {
var i TransactionsByMonth var i TransactionsByMonth
if err := rows.Scan( if err := rows.Scan(
&i.Year, &i.Date,
&i.Month,
&i.CategoryID, &i.CategoryID,
&i.BudgetID, &i.BudgetID,
&i.Amount, &i.Amount,