This is my query
Example 1
SELECT sum(percent::numeric)::numeric
FROM (
SELECT (amount::numeric * 100::numeric) / sum(amount::numeric) OVER (PARTITION BY a, b, c)::numeric as percent
FROM (
values ('a', 'b', 'c', 1, '609639'::numeric)
, ('a', 'b', 'c', 2, '72296.66'::numeric)
, ('a', 'b', 'c', 10, '174027.6'::numeric)
, ('a', 'b', 'c', 12, '154179.16'::numeric)
, ('a', 'b', 'c', 14, '97530.2'::numeric)
, ('a', 'b', 'c', 15, '1335529.7'::numeric)
, ('a', 'b', 'c', 25, '201315.1'::numeric)
, ('a', 'b', 'c', 51, '1424121.5'::numeric)
, ('a', 'b', 'c', 61, '51045.5'::numeric)
, ('a', 'b', 'c', 63, '14664.4'::numeric)
, ('a', 'b', 'c', 66, '740921.9'::numeric)
, ('a', 'b', 'c', 70, '189266.9'::numeric)
) AS sample_values (a, b, c, d, amount)
) as table_1;
sum
-------------------------
99.99999999999999997676
(1 row)
Example 2
SELECT sum(percent::numeric)::numeric
FROM (
SELECT (amount::numeric * 100::numeric) / sum(amount::numeric) OVER (PARTITION BY a, b, c)::numeric as percent
FROM (
values ('a', 'b', 'c', 2, '72296.66'::numeric)
, ('a', 'b', 'c', 10, '174027.6'::numeric)
, ('a', 'b', 'c', 12, '154179.16'::numeric)
, ('a', 'b', 'c', 15, '1335529.7'::numeric)
, ('a', 'b', 'c', 25, '201315.1'::numeric)
, ('a', 'b', 'c', 51, '1424121.5'::numeric)
, ('a', 'b', 'c', 61, '51045.5'::numeric)
, ('a', 'b', 'c', 63, '14664.4'::numeric)
, ('a', 'b', 'c', 66, '740921.9'::numeric)
, ('a', 'b', 'c', 70, '189266.9'::numeric)
) AS sample_values (a, b, c, d, amount)
) as table_1;
sum
-------------------------
100.0000000000000000583
(1 row)
I am casting everything to numeric and yet the query still gives me
Details
psql --version
psql (PostgreSQL) 13.1
postgres=# select version();
version
------------------------------------------------------------
PostgreSQL 13.1, compiled by Visual C++ build 1914, 64-bit
(1 row)
Windows 10 x64
I am expecting a clean 100 since numeric shouldn't have floating point error. What went wrong?