0
votes

This is my table:

CREATE TABLE my_schema.money
(
    amount numeric NOT NULL,
    rate jsonb
);

and example data:

INSERT INTO my_schema.money (amount, rate)
VALUES (
    15.7, 
    '{
        "date": "2017-06-25",
        "to":
        {
            "USD": 0.282451842549122,
            "GBP": 0.252381738944908
        },
        "from": "EUR"
    }'
);

I'm trying to create a view in the database, with the multiple of the column amount and the rate of USD on column rate, without any success, any ideas?

For example: 15.7 * 0.282451842549122 = 4.4344939280212154.

1

1 Answers

0
votes

Assuming your view includes the original columns:

create view my_schema.view_name_for_usd
as
select *, amount * (rate->'to'->>'USD')::numeric usd_amount 
    from my_schema.money