From 88e1dbdfde00706606de4cfd9b698bdb8181420f Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 14 Apr 2022 20:21:55 +0000 Subject: [PATCH] Simplify query by using COALESCE --- postgres/cumultative-balances.sql.go | 2 +- postgres/queries/cumultative-balances.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/postgres/cumultative-balances.sql.go b/postgres/cumultative-balances.sql.go index 98f3aed..de14e6d 100644 --- a/postgres/cumultative-balances.sql.go +++ b/postgres/cumultative-balances.sql.go @@ -19,7 +19,7 @@ SELECT COALESCE(ass.date, tra.date), COALESCE(ass.category_id, tra.category_id), COALESCE(tra.amount, 0)::decimal(12,2) as transactions FROM assignments_by_month as ass FULL OUTER JOIN transactions_by_month as tra ON ass.date = tra.date AND ass.category_id = tra.category_id -WHERE (ass.budget_id IS NULL OR ass.budget_id = $1) AND (tra.budget_id IS NULL OR tra.budget_id = $1) +WHERE COALESCE(ass.budget_id, tra.budget_id) = $1 ORDER BY COALESCE(ass.date, tra.date), COALESCE(ass.amount, tra.amount) ` diff --git a/postgres/queries/cumultative-balances.sql b/postgres/queries/cumultative-balances.sql index cc22480..4803d8b 100644 --- a/postgres/queries/cumultative-balances.sql +++ b/postgres/queries/cumultative-balances.sql @@ -4,5 +4,5 @@ SELECT COALESCE(ass.date, tra.date), COALESCE(ass.category_id, tra.category_id), COALESCE(tra.amount, 0)::decimal(12,2) as transactions FROM assignments_by_month as ass FULL OUTER JOIN transactions_by_month as tra ON ass.date = tra.date AND ass.category_id = tra.category_id -WHERE (ass.budget_id IS NULL OR ass.budget_id = @budget_id) AND (tra.budget_id IS NULL OR tra.budget_id = @budget_id) +WHERE COALESCE(ass.budget_id, tra.budget_id) = @budget_id ORDER BY COALESCE(ass.date, tra.date), COALESCE(ass.amount, tra.amount); \ No newline at end of file