Reputation: 691
I have a table called "Audio" with a column "transcript" as the following:
{"transcript": [
{"p": 0, "s": 0, "e": 320, "c": 0.545, "w": "This"},
{"p": 1, "s": 320, "e": 620, "c": 0.825, "w": "call"},
{"p": 2, "s": 620, "e": 780, "c": 0.909, "w": "is"},
{"p": 3, "s": 780, "e": 1010, "c": 0.853, "w": "being"}
...
]}
I would like to get the value of "p" where "w" matches certain keywords.
If I do the following query, it will give me the entire 's' entries of Audio where one of its "w" has words "google" or "all."
select json_array_elements(transcript->'transcript')->>'s'
from Audio,
json_array_elements(transcript->'transcript') as temp
where temp->>'w' ilike any(array['all','google'])
How could I get only value of "p" where the condition is satisfied?
Edit: How could I get the value of "p" and its corresponding Audio ID at the same time?
Upvotes: 0
Views: 920
Reputation: 7286
Select your transcript array elements into a common table expression and match from there:
WITH transcript AS (
SELECT json_array_elements((transcript -> 'transcript')) AS line
FROM audio
)
SELECT line ->> 'p'
FROM transcript
WHERE line ->> 'w' ILIKE ANY (ARRAY ['all', 'google']);
This will select matching lines from all rows in the audio table. I'm guessing that you'll want to restrict the results to a subset of rows, in which case you'll have to narrow the query. Assuming an id
column, do something like this:
WITH transcript AS (
SELECT
id,
json_array_elements((transcript -> 'transcript')) AS line
FROM audio
WHERE id = 1
)
SELECT
id,
line ->> 'p'
FROM transcript
WHERE line ->> 'w' ILIKE ANY (ARRAY ['call', 'google'])
Upvotes: 1