1

I have a table which contains a json column that has an array field, I want to check if the numbers inside the array belong to a range, that is if each element passes the predicate to be greater than X and lesser than Y,

select nom.id, nom.doc
from nom
join json_each(nom.doc, "$.rate") e
where e.value > 0 and e.value < 7

I'd expect this to work but it doesn't, I understand that json_each returns a table, and I think maybe using grouping or aggregation I could get what I want, but I want to know if there is another way, I would like to not rely on aggregation, as I would want to for example, check if all elements pass a predicate, for example, if all are greater than X, or if I where working with strings, if they all match a pattern etc.

sample data:

id: 1, doc: {rate: [1, 2, 3]}
id: 2, doc: {rate: [1, 2, 3, 5 9]}

If I have those two records, I'd like to get those whose rate has values lesser than 7 and greater than 0, for example, record with id 2 should not appear in the query I desire.

2
  • Post sample data of your table to clarify what you want. Commented May 1, 2022 at 7:40
  • Hi @forpas, just did it. Commented May 1, 2022 at 7:43

1 Answer 1

1

One way to do it is to extract all the values of the array inside a CTE and use NOT EXISTS in the main query to check if all values of a certain id pass the predicate:

WITH cte AS (
  SELECT n.id, n.doc, e.value
  FROM nom n JOIN json_each(n.doc, "$.rate") e
)
SELECT n.id, n.doc
FROM nom n
WHERE NOT EXISTS (
  SELECT 1
  FROM cte c
  WHERE c.id = n.id AND NOT <your predicate>
);

Change <your predicate> to something like:

(c.value > 0 AND c.value < 7)

See the demo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.