0

So I have written a postgreSQL function that is supposed to do a search on a table based on a huge amount of optional input parameters which i group with lots of AND statements. This one however:

AND
(
 (newcheck IS NULL) 
    OR
    ( 
    newcheck IS NOT NULL AND product.id IN(
    CASE WHEN newcheck='New' 
        THEN
        (SELECT product.id FROM product WHERE product.anew IS true)
    ELSE
        (SELECT product.id from product WHERE product.anew IS false)
    END)
    )
)

gives me a

ERROR:  more than one row returned by a subquery used as an expression

This isnt helping much since I do want it to return a lot more than one row. The values of the newcheck variable will be sent from a dropdown menu in a web form so it can only be 'New' or 'Old'. Any ideas on what might be causing this problem?

1
  • The problem - every subselect returns more than one row and CASE statement expects one result in every branch. Commented Aug 8, 2013 at 8:39

1 Answer 1

1

Try something like:

AND ((newcheck IS NULL)
     OR (newcheck IS NOT NULL 
         AND product.id IN (SELECT product.id 
                            FROM product 
                            WHERE product.anew = CASE WHEN newcheck='New' 
                                                      THEN true 
                                                      ELSE false
                                                 END))
Sign up to request clarification or add additional context in comments.

1 Comment

Alright that seems to work perfectly when I set the newcheck to anything but 'New'. In the case where it is 'New' I only get one row as a result...

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.