0
CREATE FUNCTION test(VARIADIC arr text[]) 
RETURNS TABLE (data_time TIMESTAMPTZ, id text, data jsonb) 
    AS 'SELECT data_timestamp, key, value 
       FROM hit_count 
       CROSS JOIN jsonb_each(data) 
       WHERE key = ALL ($1)' 
LANGUAGE SQL;

If I call the function above with select test('123') it works fine. If I call it with select test('123','234') it returns nothing ie

test 
------
(0 rows)

However if I define it as

CREATE FUNCTION test(VARIADIC arr text[]) 
RETURNS TABLE (data_time TIMESTAMPTZ, id text, data jsonb) 
    AS 'SELECT data_timestamp, key, value 
        FROM hit_count 
        CROSS JOIN jsonb_each(data) 
        WHERE key != ALL ($1)' 
LANGUAGE SQL;

then the function returns all the data but those that fit the condition

Any ideas??

4
  • I don't know how Postgres handles parameters but perhaps you just want in ($1)? Commented Sep 18, 2022 at 6:41
  • @shawnt00 thats a no go. I tried it... also the last comment on this post stackoverflow.com/questions/17138792/… suggest that the operator use is correct. So Im not sure ERROR: operator does not exist: text = text[] LINE 1: ...CROSS JOIN jsonb_each(data) WHERE key IN ($1)' L... Commented Sep 18, 2022 at 6:45
  • @shawnt00 I id'd the problem. Answer below. Ty for your time Commented Sep 18, 2022 at 7:02
  • Makes sense. I should have even suggested that. Commented Sep 18, 2022 at 16:51

1 Answer 1

1

ALL was used as opposed to ANY the corrected function looks like this

CREATE FUNCTION test(VARIADIC arr text[]) 
RETURNS TABLE (data_time TIMESTAMPTZ, id text, data jsonb) 
    AS 'SELECT data_timestamp, key, value 
        FROM hit_count 
        CROSS JOIN jsonb_each(data) 
        WHERE key = ANY ($1)' 
LANGUAGE SQL;
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.