diff --git a/postgres/categories.sql.go b/postgres/categories.sql.go index 6d1d14c..3377752 100644 --- a/postgres/categories.sql.go +++ b/postgres/categories.sql.go @@ -165,12 +165,13 @@ type GetCategoriesWithBalanceParams struct { } type GetCategoriesWithBalanceRow struct { - ID uuid.UUID - Name string - Group string - Available Numeric - Activity Numeric - Assigned Numeric + ID uuid.UUID + Name string + Group string + Available Numeric + AvailableLastMonth Numeric + Activity Numeric + Assigned Numeric } func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) { @@ -187,6 +188,7 @@ func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategorie &i.Name, &i.Group, &i.Available, + &i.AvailableLastMonth, &i.Activity, &i.Assigned, ); err != nil { diff --git a/postgres/queries/categories.sql b/postgres/queries/categories.sql index a1d1546..a322f22 100644 --- a/postgres/queries/categories.sql +++ b/postgres/queries/categories.sql @@ -21,37 +21,32 @@ WHERE category_groups.budget_id = $1; -- 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 < @to_date - ) - , 0)+COALESCE( - ( - SELECT SUM(t_hist.amount) - FROM transactions t_hist - WHERE categories.id = t_hist.category_id - AND t_hist.date < @to_date - ) - , 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 >= @from_date AND t_this.date < @to_date - ) - , 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 >= @from_date AND a_hist.date < @to_date - ) - ,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 < @to_date + ), 0)+COALESCE(( + SELECT SUM(t_hist.amount) FROM transactions t_hist + WHERE categories.id = t_hist.category_id AND t_hist.date < @to_date + ), 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 < @from_date + ), 0)+COALESCE(( + SELECT SUM(t_hist.amount) FROM transactions t_hist + WHERE categories.id = t_hist.category_id AND t_hist.date < @from_date + ), 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 >= @from_date AND t_this.date < @to_date + ), 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 >= @from_date AND a_hist.date < @to_date + ), 0)::decimal(12,2) as assigned FROM categories INNER JOIN category_groups ON categories.category_group_id = category_groups.id