-- -- ec_total_shipping/1 -- create or replace function ec_total_shipping( integer ) returns numeric as $$ DECLARE v_order_id alias for $1; order_shipping numeric; item_shipping numeric; BEGIN select into order_shipping coalesce(shipping_charged,0) - coalesce(shipping_refunded,0) FROM ec_orders WHERE order_id=v_order_id; select into item_shipping coalesce(sum(shipping_charged),0) - coalesce(sum(shipping_refunded),0) FROM ec_items WHERE order_id=v_order_id and item_state <> 'void'; return order_shipping + item_shipping; END;$$ language plpgsql;