-- -- ec_total_price/1 -- create or replace function ec_total_price( integer ) returns numeric as $$ DECLARE v_order_id alias for $1; price numeric; BEGIN select into price coalesce(sum(price_charged),0) - coalesce(sum(price_refunded),0) FROM ec_items WHERE order_id=v_order_id and item_state <> 'void'; return price; END;$$ language plpgsql;