# Compute the required details in a table using DECIMAL(38,2) for dollars and cents. create or replace function bill( quantities, prices, taxrate) as table ( with cte as (select sum(q*p) as subtotal from (select unnest(quantities) as q, unnest(prices) as p)), details as (select subtotal, subtotal * taxrate as tax, (subtotal + tax ) as total from cte) select subtotal::DECIMAL(38,2) as subtotal, tax::DECIMAL(38,2) as tax, total::DECIMAL(38,2) as total from details ) ; # The order and tax rate: from bill ( [4000000000000000, 2], [5.50, 2.86], 0.0765);