A natural approach is to get the sums in a single row:
with my_table(data) as (
values
('{"BTC": "0.1"}'::jsonb),
('{"BTC": "0.2", "DOGE": "2.0"}'),
('{"BTC": "0.3"}')
)
select
sum((data->>'BTC')::numeric) as btc,
sum((data->>'DOGE')::numeric) as doge
from my_table;
btc | doge
-----+------
0.6 | 2.0
(1 row)
If you insist on a columnar presentation:
with my_table(data) as (
values
('{"BTC": "0.1"}'::jsonb),
('{"BTC": "0.2", "DOGE": "2.0"}'),
('{"BTC": "0.3"}')
)
select code, sum((data->>code)::numeric)
from my_table
cross join unnest(array['BTC', 'DOGE']) as code
group by code
order by code;
code | sum
------+-----
BTC | 0.6
DOGE | 2.0
(2 rows)