Add views with results grouped by month
This commit is contained in:
parent
a0ebdd01aa
commit
c3a022b595
@ -52,3 +52,38 @@ func (q *Queries) DeleteAllAssignments(ctx context.Context, budgetID uuid.UUID)
|
|||||||
}
|
}
|
||||||
return result.RowsAffected()
|
return result.RowsAffected()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getAssignmentsByMonthAndCategory = `-- name: GetAssignmentsByMonthAndCategory :many
|
||||||
|
SELECT year, month, category_id, budget_id, amount
|
||||||
|
FROM assignments_by_month
|
||||||
|
WHERE assignments_by_month.budget_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetAssignmentsByMonthAndCategory(ctx context.Context, budgetID uuid.UUID) ([]AssignmentsByMonth, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, getAssignmentsByMonthAndCategory, budgetID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []AssignmentsByMonth
|
||||||
|
for rows.Next() {
|
||||||
|
var i AssignmentsByMonth
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.Year,
|
||||||
|
&i.Month,
|
||||||
|
&i.CategoryID,
|
||||||
|
&i.BudgetID,
|
||||||
|
&i.Amount,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
@ -24,6 +24,14 @@ type Assignment struct {
|
|||||||
Amount Numeric
|
Amount Numeric
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AssignmentsByMonth struct {
|
||||||
|
Year float64
|
||||||
|
Month float64
|
||||||
|
CategoryID uuid.UUID
|
||||||
|
BudgetID uuid.UUID
|
||||||
|
Amount int64
|
||||||
|
}
|
||||||
|
|
||||||
type Budget struct {
|
type Budget struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
Name string
|
Name string
|
||||||
@ -59,6 +67,14 @@ type Transaction struct {
|
|||||||
PayeeID uuid.NullUUID
|
PayeeID uuid.NullUUID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TransactionsByMonth struct {
|
||||||
|
Year float64
|
||||||
|
Month float64
|
||||||
|
CategoryID uuid.NullUUID
|
||||||
|
BudgetID uuid.UUID
|
||||||
|
Amount int64
|
||||||
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
Email string
|
Email string
|
||||||
|
@ -11,3 +11,8 @@ DELETE FROM assignments
|
|||||||
USING categories
|
USING 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
|
||||||
WHERE categories.id = assignments.category_id AND category_groups.budget_id = @budget_id;
|
WHERE categories.id = assignments.category_id AND category_groups.budget_id = @budget_id;
|
||||||
|
|
||||||
|
-- name: GetAssignmentsByMonthAndCategory :many
|
||||||
|
SELECT *
|
||||||
|
FROM assignments_by_month
|
||||||
|
WHERE assignments_by_month.budget_id = @budget_id;
|
@ -33,3 +33,8 @@ DELETE FROM transactions
|
|||||||
USING accounts
|
USING accounts
|
||||||
WHERE accounts.budget_id = @budget_id
|
WHERE accounts.budget_id = @budget_id
|
||||||
AND accounts.id = transactions.account_id;
|
AND accounts.id = transactions.account_id;
|
||||||
|
|
||||||
|
-- name: GetTransactionsByMonthAndCategory :many
|
||||||
|
SELECT *
|
||||||
|
FROM transactions_by_month
|
||||||
|
WHERE transactions_by_month.budget_id = @budget_id;
|
17
postgres/schema/202112081529_views-for-months.sql
Normal file
17
postgres/schema/202112081529_views-for-months.sql
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
-- +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
|
||||||
|
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;
|
||||||
|
|
||||||
|
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
|
||||||
|
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;
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
DROP VIEW transactions_by_month;
|
||||||
|
DROP VIEW assignments_by_month;
|
@ -63,6 +63,41 @@ func (q *Queries) DeleteAllTransactions(ctx context.Context, budgetID uuid.UUID)
|
|||||||
return result.RowsAffected()
|
return result.RowsAffected()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getTransactionsByMonthAndCategory = `-- name: GetTransactionsByMonthAndCategory :many
|
||||||
|
SELECT year, month, category_id, budget_id, amount
|
||||||
|
FROM transactions_by_month
|
||||||
|
WHERE transactions_by_month.budget_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetTransactionsByMonthAndCategory(ctx context.Context, budgetID uuid.UUID) ([]TransactionsByMonth, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, getTransactionsByMonthAndCategory, budgetID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []TransactionsByMonth
|
||||||
|
for rows.Next() {
|
||||||
|
var i TransactionsByMonth
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.Year,
|
||||||
|
&i.Month,
|
||||||
|
&i.CategoryID,
|
||||||
|
&i.BudgetID,
|
||||||
|
&i.Amount,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many
|
const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many
|
||||||
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount,
|
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount,
|
||||||
accounts.name as account, COALESCE(payees.name, '') as payee, COALESCE(category_groups.name, '') as category_group, COALESCE(categories.name, '') as category
|
accounts.name as account, COALESCE(payees.name, '') as payee, COALESCE(category_groups.name, '') as category_group, COALESCE(categories.name, '') as category
|
||||||
|
Loading…
x
Reference in New Issue
Block a user