Simplify query
This commit is contained in:
parent
07c0c56d11
commit
777520e9df
@ -48,8 +48,8 @@ FROM transactions_by_month
|
|||||||
WHERE transactions_by_month.budget_id = @budget_id;
|
WHERE transactions_by_month.budget_id = @budget_id;
|
||||||
|
|
||||||
-- name: GetProblematicTransactions :many
|
-- name: GetProblematicTransactions :many
|
||||||
SELECT *
|
SELECT transactions.*
|
||||||
FROM transactions
|
FROM display_transactions AS transactions
|
||||||
LEFT JOIN accounts
|
LEFT JOIN accounts
|
||||||
ON transactions.account_id = accounts.id
|
ON transactions.account_id = accounts.id
|
||||||
LEFT JOIN transactions AS otherGroupTransaction
|
LEFT JOIN transactions AS otherGroupTransaction
|
||||||
@ -57,7 +57,7 @@ LEFT JOIN transactions AS otherGroupTransaction
|
|||||||
AND transactions.id != otherGroupTransaction.id
|
AND transactions.id != otherGroupTransaction.id
|
||||||
AND transactions.account_id != otherGroupTransaction.account_id
|
AND transactions.account_id != otherGroupTransaction.account_id
|
||||||
LEFT JOIn accounts AS otherGroupAccount
|
LEFT JOIn accounts AS otherGroupAccount
|
||||||
ON otherGroupTransaction.account_id = accounts.id
|
ON otherGroupTransaction.account_id = otherGroupAccount.id
|
||||||
WHERE transactions.category_id IS NULL
|
WHERE transactions.category_id IS NULL
|
||||||
AND accounts.on_budget
|
AND accounts.on_budget
|
||||||
AND (otherGroupAccount.id IS NULL OR NOT otherGroupAccount.on_budget)
|
AND (otherGroupAccount.id IS NULL OR NOT otherGroupAccount.on_budget)
|
||||||
|
@ -7,7 +7,6 @@ package postgres
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
||||||
@ -119,8 +118,8 @@ func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getProblematicTransactions = `-- name: GetProblematicTransactions :many
|
const getProblematicTransactions = `-- name: GetProblematicTransactions :many
|
||||||
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.category_id, transactions.payee_id, transactions.group_id, transactions.status, accounts.id, accounts.budget_id, accounts.name, accounts.on_budget, accounts.is_open, accounts.last_reconciled, othergrouptransaction.id, othergrouptransaction.date, othergrouptransaction.memo, othergrouptransaction.amount, othergrouptransaction.account_id, othergrouptransaction.category_id, othergrouptransaction.payee_id, othergrouptransaction.group_id, othergrouptransaction.status, othergroupaccount.id, othergroupaccount.budget_id, othergroupaccount.name, othergroupaccount.on_budget, othergroupaccount.is_open, othergroupaccount.last_reconciled
|
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.group_id, transactions.status, transactions.account, transactions.payee_id, transactions.category_id, transactions.payee, transactions.category_group, transactions.category, transactions.transfer_account, transactions.budget_id, transactions.account_id
|
||||||
FROM transactions
|
FROM display_transactions AS transactions
|
||||||
LEFT JOIN accounts
|
LEFT JOIN accounts
|
||||||
ON transactions.account_id = accounts.id
|
ON transactions.account_id = accounts.id
|
||||||
LEFT JOIN transactions AS otherGroupTransaction
|
LEFT JOIN transactions AS otherGroupTransaction
|
||||||
@ -128,86 +127,38 @@ LEFT JOIN transactions AS otherGroupTransaction
|
|||||||
AND transactions.id != otherGroupTransaction.id
|
AND transactions.id != otherGroupTransaction.id
|
||||||
AND transactions.account_id != otherGroupTransaction.account_id
|
AND transactions.account_id != otherGroupTransaction.account_id
|
||||||
LEFT JOIn accounts AS otherGroupAccount
|
LEFT JOIn accounts AS otherGroupAccount
|
||||||
ON otherGroupTransaction.account_id = accounts.id
|
ON otherGroupTransaction.account_id = otherGroupAccount.id
|
||||||
WHERE transactions.category_id IS NULL
|
WHERE transactions.category_id IS NULL
|
||||||
AND accounts.on_budget
|
AND accounts.on_budget
|
||||||
AND (otherGroupAccount.id IS NULL OR NOT otherGroupAccount.on_budget)
|
AND (otherGroupAccount.id IS NULL OR NOT otherGroupAccount.on_budget)
|
||||||
AND accounts.budget_id = $1
|
AND accounts.budget_id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
type GetProblematicTransactionsRow struct {
|
func (q *Queries) GetProblematicTransactions(ctx context.Context, budgetID uuid.UUID) ([]DisplayTransaction, error) {
|
||||||
ID uuid.UUID
|
|
||||||
Date time.Time
|
|
||||||
Memo string
|
|
||||||
Amount numeric.Numeric
|
|
||||||
AccountID uuid.UUID
|
|
||||||
CategoryID uuid.NullUUID
|
|
||||||
PayeeID uuid.NullUUID
|
|
||||||
GroupID uuid.NullUUID
|
|
||||||
Status TransactionStatus
|
|
||||||
ID_2 uuid.NullUUID
|
|
||||||
BudgetID uuid.NullUUID
|
|
||||||
Name sql.NullString
|
|
||||||
OnBudget sql.NullBool
|
|
||||||
IsOpen sql.NullBool
|
|
||||||
LastReconciled sql.NullTime
|
|
||||||
ID_3 uuid.UUID
|
|
||||||
Date_2 time.Time
|
|
||||||
Memo_2 string
|
|
||||||
Amount_2 numeric.Numeric
|
|
||||||
AccountID_2 uuid.UUID
|
|
||||||
CategoryID_2 uuid.NullUUID
|
|
||||||
PayeeID_2 uuid.NullUUID
|
|
||||||
GroupID_2 uuid.NullUUID
|
|
||||||
Status_2 TransactionStatus
|
|
||||||
ID_4 uuid.NullUUID
|
|
||||||
BudgetID_2 uuid.NullUUID
|
|
||||||
Name_2 sql.NullString
|
|
||||||
OnBudget_2 sql.NullBool
|
|
||||||
IsOpen_2 sql.NullBool
|
|
||||||
LastReconciled_2 sql.NullTime
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *Queries) GetProblematicTransactions(ctx context.Context, budgetID uuid.UUID) ([]GetProblematicTransactionsRow, error) {
|
|
||||||
rows, err := q.db.QueryContext(ctx, getProblematicTransactions, budgetID)
|
rows, err := q.db.QueryContext(ctx, getProblematicTransactions, budgetID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
var items []GetProblematicTransactionsRow
|
var items []DisplayTransaction
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var i GetProblematicTransactionsRow
|
var i DisplayTransaction
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&i.ID,
|
&i.ID,
|
||||||
&i.Date,
|
&i.Date,
|
||||||
&i.Memo,
|
&i.Memo,
|
||||||
&i.Amount,
|
&i.Amount,
|
||||||
&i.AccountID,
|
|
||||||
&i.CategoryID,
|
|
||||||
&i.PayeeID,
|
|
||||||
&i.GroupID,
|
&i.GroupID,
|
||||||
&i.Status,
|
&i.Status,
|
||||||
&i.ID_2,
|
&i.Account,
|
||||||
|
&i.PayeeID,
|
||||||
|
&i.CategoryID,
|
||||||
|
&i.Payee,
|
||||||
|
&i.CategoryGroup,
|
||||||
|
&i.Category,
|
||||||
|
&i.TransferAccount,
|
||||||
&i.BudgetID,
|
&i.BudgetID,
|
||||||
&i.Name,
|
&i.AccountID,
|
||||||
&i.OnBudget,
|
|
||||||
&i.IsOpen,
|
|
||||||
&i.LastReconciled,
|
|
||||||
&i.ID_3,
|
|
||||||
&i.Date_2,
|
|
||||||
&i.Memo_2,
|
|
||||||
&i.Amount_2,
|
|
||||||
&i.AccountID_2,
|
|
||||||
&i.CategoryID_2,
|
|
||||||
&i.PayeeID_2,
|
|
||||||
&i.GroupID_2,
|
|
||||||
&i.Status_2,
|
|
||||||
&i.ID_4,
|
|
||||||
&i.BudgetID_2,
|
|
||||||
&i.Name_2,
|
|
||||||
&i.OnBudget_2,
|
|
||||||
&i.IsOpen_2,
|
|
||||||
&i.LastReconciled_2,
|
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user