It looks like we may not have to go full-on window function to fix those. Consider the following test of a redefinition of the money.open_transaction_billing_type_summary view:
-- Given a billable transaction exhibiting the issue, replace XXXX below.
SELECT
billing.xact,
last(billing.billing_type ORDER BY billing.billing_ts) AS last_billing_type,
last(billing.note ORDER BY billing.billing_ts) AS last_billing_note,
max(billing.billing_ts) AS last_billing_ts,
sum(COALESCE(billing.amount, 0::numeric)) AS total_owed
FROM money.billing
WHERE billing.voided IS FALSE AND billing.xact = XXXX
GROUP BY billing.xact, billing.billing_type;
In my testing, this is just as fast as the one not using the ORDER BY clause in the aggregate functions.
Chris,
It looks like we may not have to go full-on window function to fix those. Consider the following test of a redefinition of the money.open_ transaction_ billing_ type_summary view:
-- Given a billable transaction exhibiting the issue, replace XXXX below.
SELECT billing. billing_ type ORDER BY billing.billing_ts) AS last_billing_type, billing. note ORDER BY billing.billing_ts) AS last_billing_note, billing. billing_ ts) AS last_billing_ts, COALESCE( billing. amount, 0::numeric)) AS total_owed billing_ type;
billing.xact,
last(
last(
max(
sum(
FROM money.billing
WHERE billing.voided IS FALSE AND billing.xact = XXXX
GROUP BY billing.xact, billing.
In my testing, this is just as fast as the one not using the ORDER BY clause in the aggregate functions.