Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CASE...WHEN in WHERE clause in Postgresql

Tags:

My query looks like:

SELECT * 
FROM table 
WHERE t1.id_status_notatka_1 = ANY (selected_type)  
 AND t1.id_status_notatka_2 = ANY (selected_place) 

here I would like to add CASE WHEN so my query is:

    SELECT *  
    FROM table 
    WHERE t1.id_status_notatka_1 = ANY (selected_type)  
      AND t1.id_status_notatka_2    = ANY (selected_place) 
      AND CASE 
            WHEN t2.id_bank = 12 THEN t1.id_status_notatka_4 = ANY (selected_effect) 
         END

but it doesn't work. The syntax is good but it fails in searching for anything. So my question is - how use CASE WHEN in WHERE clause. Short example: if a=0 then add some condition to WHERE (AND condition), if it's not then don't add (AND condition)

like image 625
Michu93 Avatar asked Aug 29 '17 14:08

Michu93


People also ask

Can I use case in WHERE clause PostgreSQL?

The PostgreSQL CASE expression is the same as IF/ELSE statement in other programming languages. It allows you to add if-else logic to the query to form a powerful query. Since CASE is an expression, you can use it in any places where an expression can be used e.g., SELECT , WHERE , GROUP BY , and HAVING clause.

Can you use case when in a WHERE clause?

CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.

How do you write a case statement in PostgreSQL?

Syntax: CASE WHEN boolean-expression-1 THEN statements [ WHEN boolean-expression-2 THEN statements ... ] [ ELSE statements ] END CASE; The searched CASE statement executes statements based on the result of Boolean expressions in each WHEN clause.

How do you write multiple case statements in PostgreSQL?

PostgreSQL CASE when multiple conditions We can apply multiple conditions in our CASE expression by using multiple WHEN and THEN statements. We can also use these CASE expressions within aggregate functions such as SUM, MIN, MAX, etc.


2 Answers

No need for CASE EXPRESSION , simply use OR with parenthesis :

AND (t2.id_bank <> 12 OR t1.id_status_notatka_4 = ANY (selected_effect))
like image 96
sagi Avatar answered Sep 30 '22 04:09

sagi


The accepted answer works, but I'd like to share input for those who are looking for a different answer. Thanks to sagi, I've come up with the following query, but I'd like to give a test case as well.

Let us assume this is the structure of our table

tbl
id   | type     | status
-----------------------
1    | Student  | t
2    | Employee | f
3    | Employee | t
4    | Student  | f

and we want to select all Student rows, that have Status = 't', however, We also like to retrieve all Employee rows regardless of its Status.

if we perform SELECT * FROM tbl WHERE type = 'Student' AND status = 't' we would only get the following result, we won't be able to fetch Employees

tbl
id   | type     | status
-----------------------
1    | Student  | t

and performing SELECT * FROM tbl WHERE Status = 't' we would only get the following result, we got an Employee Row on the result but there are Employee Rows that were not included on the result set, one could argue that performing IN might work, however, it will give the same result set. SELECT * FROM tbl WHERE type IN('Student', 'Employee') AND status = 't'

tbl
id   | type     | status
-----------------------
1    | Student  | t
3    | Employee | t

remember, we want to retrieve all Employee rows regardless of its Status, to do that we perform the query

SELECT * FROM tbl WHERE (type = 'Student' AND status = 't') OR (type = 'Employee')

result will be

table
id   | type     | status
-----------------------
1    | Student  | t
2    | Employee | f
3    | Employee | t
like image 31
Mo Chan Avatar answered Sep 30 '22 04:09

Mo Chan