Comment 1 for bug 1704391

Revision history for this message
Mike Rylander (mrylander) wrote :

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.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.