0

I have a jsonb parameter to my plsql procedure with data {1=2,2=3}. My Table contains the following data,

att_name |att_key |att_value
abcd     |1       |2
bcde     |2       |3
efgh     |1       |3

I want to filter out the data where (att_key = 1 and att_value = 2) or (att_key = 2 and att_value = 3) which should be dynamically generated based on the jsonb input.The output will contain abcd and bcde only. Is there any way to create this query dynamically in postgresql.

1 Answer 1

1

{1=2,2=3} is not a valid JSON.

I think you mean {"1":"2","2":"3"}.

You may use json_each_text to convert to key value pair (text) and compare easily.

SELECT * 
  FROM   t 
WHERE  ( att_key, att_value ) IN (SELECT inp.key :: INT, inp.VALUE :: INT 
     FROM   json_each_text('{"1":"2","2":"3"}') AS inp ); 

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.