PostgreSql : Json Array to Rows using Lateral Join

前端 未结 4 1016
误落风尘
误落风尘 2020-12-12 06:17

I have two following JSON Array in details field of my table and need to evaluate the query as I use in another relational table.

{
    \"city\": \"London\",         


        
4条回答
  •  感动是毒
    2020-12-12 06:36

    Is this what you want ?

        -- just simulate table:
    with my_table(details) as(
    values
    ('{
    "city": "London",
    "name": "Sainburry",
    "quantities": [112, 145, 222, 122, 124],
    "prices": [4, 4, 4, 0, 3],
    "dates": ["13.05.2020", "14.05.2020", "15.05.2020", "16.05.2020", "17.05.2020"]
    }'::json)
    )
    
    
    -- here is query:
    select  
    my_table.details->>'city',  u.quantities, u.prices  
    from my_table
    JOIN LATERAL UNNEST( 
        ARRAY(SELECT json_array_elements_text(details->'quantities')) ,
        ARRAY(SELECT json_array_elements_text(details->'prices')) 
    ) u(quantities, prices) ON TRUE
    WHERE
    my_table.details->>'city' = 'London'
    

    See demo

提交回复
热议问题