Considering my basic knowledge of PostgreSQL I am unable to find out why my second statement for where clause (record->'details'->>'amount')::numeric(19,2) = 2.99
is not working with current query. Idea is to send common filter from UI for header and details and when I am trying to filter to inner array details Json column
its not giving me a exact result though it has some data within it
WITH t as (select row_to_json(rr)::jsonb as record
from (
select (
select row_to_json(row)
from (
select
c.customer_id,
c.first_name,
c.last_name,
c.email,
count(*) total,
sum(p2.amount) total_amount
from payment p2
where p2.customer_id = c.customer_id
) row
) as header,
(select array_to_json(array_agg(row_to_json(row)))
from (
select
amount,
payment_date
from payment p1
where p1.customer_id = c.customer_id
) row
) as details
from customer c
join payment p on p.customer_id = c.customer_id
) rr)
SELECT record FROM t
where record->'header'->>'customer_id'= '2'
and (record->'details'->>'amount')::numeric(19,2)= 2.99
Best Answer
It would be performance suicide to first build a huge
jsonb
object from joining two tables, and then filter a single record representing an underlying row. You would filter first, of course.Going out on a limb, this might be what you are trying to do:
There is no
record->'details'->>'amount'
in your object. There are array elements likerecord->'details'->0->>'amount'
, but those make no sense with the=
operator. So I assumed you are aiming fortotal_amount
.A little less confusing with
jsonb_build_object()
: