0

I would like to get the count of all products under a certain id.

I tried:

SELECT
    count(jsonb_object_keys(data #> '{products}')) as numProducts 
FROM
    appointment_intakes 
WHERE
    appointment_intakes.id = 'VGDKMjdxn'

I get:

ERROR:  set-valued function called in context that cannot accept a set

When I run the above without the count function, I get a list of keys.

Any idea what I am doing wrong?

Thank you.

1
  • I'm not a 100% on this one but I don't believe that is how you use the select count in Postgres... Usually it would be something like Select count(*).. Commented Oct 9, 2015 at 13:05

1 Answer 1

2

Set-valued function has to be called in FROM clause. Use lateral join:

SELECT
    count(keys) as numProducts 
FROM
    appointment_intakes, 
    LATERAL jsonb_object_keys(data #> '{products}') keys
WHERE
    appointment_intakes.id = 'VGDKMjdxn'
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.