0

I have the following query

SELECT DISTINCT ON (user_id) user_id, timestamp
FROM entries 
WHERE user_id in (1,2)
AND entry_type IN(
  SELECT jsonb_array_elements_text(
    SELECT entry_types 
    FROM users INNER JOIN orgs
    ON org_id = orgs.id 
    WHERE users.id = 1
  )
);

I'm getting a syntax error at or near select

syntax error at or near "select" LINE 1: ... entry_type in( select jsonb_array_elements_text(select ent.

The field entry_types is a JSONB field, so I am trying to convert it to text in order to use it in the WHERE IN clause.

PostgreSQL 13.0

This sub-query within jsonb_array_elements_text

SELECT entry_types 
    FROM users INNER JOIN orgs
    ON org_id = orgs.id 
    WHERE users.id = 1

Returns a single JSONB entry like this:

                entry_types                     
--------------------------------------------  
["type1", "type2", "type3"]

I'm simply trying to use the array of text values returned there as the criteria inside the WHERE IN clause.

2
  • Please provide sample data, desired results and an explanation of the purpose of the query. It is not obvious. Commented Nov 6, 2020 at 18:31
  • @GMB I added some detail, let me know if that's sufficient. Commented Nov 6, 2020 at 18:36

1 Answer 1

0

The syntax error seems to point somewhere else, so maybe I am wrong, but the problem I see is a missing pair of parentheses around the subquery:

jsonb_array_elements_text((SELECT ...))
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.