I am facing issue while filtering data with array
I have columns userid,event_name,attributes,ti
Attributes column have value like this
{"bool_sample":true,"array_int":[10,20,25,38],"array_string":["hello","world","i am fine"]}
My query
SELECT * FROM "events-data" CROSS JOIN UNNEST(CAST(json_extract(attributes, '$["array_int"]') AS array<int>)) AS t(val)
WHERE val > 9;
This query filtering data but it giving me multiple row for same record, for above attributes column record it giving me 4 rows
userid,event_name,attributes,ti,val
test-userid,test-event,test-attributes,10
test-userid,test-event,test-attributes,20
test-userid,test-event,test-attributes,25
test-userid,test-event,test-attributes,38
I do not need multiple rows
2
Answers
No need CROSS JOIN with UNNEST extracted data. Check
exists
for.Try
That is what
UNNEST
does – it expands array in multiple rows (one row per array element, see the docs).If the goal is to fetch rows with
array_int
containing values more than 9 then you can use JSON functions, for examplejson_exists
:Output:
If you need to get the array itself with filtered data then you can use the casting+filtering:
Output: