# SQL "Where" clause "Case" with multiple values in "Then"

**URL:** <https://forum.inductiveautomation.com/t/sql-where-clause-case-with-multiple-values-in-then/63624>\
**Category:** Ignition\
**Created:** [August 30, 2022, 7:42pm UTC](https://forum.inductiveautomation.com/t/sql-where-clause-case-with-multiple-values-in-then/63624 "2022-08-30T19:42:01Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 31, 2022, 1:13pm UTC](https://forum.inductiveautomation.com/t/sql-where-clause-case-with-multiple-values-in-then/63624/2 "2022-08-31T13:13:13Z")

</div>

I take advantage of short circuit logic in databases a lot with the parameters inside of Ignition. I’m not quite sure what you are trying to do here as it appears your equalities for your three cases are all identical `code!= Case when :machine=1 or :machine=2 or (:machine >3 and :machine <7) or (:machine>7 and :machine<13) ` so I would imagine the first one will always be the one that executes.

However, quick example and hopefully it will set you on the right path. Imagine I have that is listing all sales orders associated with a customer, and a dropdown menu that lets me select what customer’s sales orders to see. The caveat being that if no one is selected (the dropdown is on selected All/selectedValue is -1), then I want to see all sales orders.

I can set that up like this -  
assuming that :customerId is a integer parameter

```auto
SELECT * 
FROM salesOrders
WHERE (:customerId =-1 OR salesOrders.parentCustomerId=:customerId)

```

So if `:customerId` is negative -1, that first clause of the OR statement is True and the second one is never evaluated. But if it `:customerId=-1` is false, then the filtering occurs.

You can do a similar and opposite with AND statements. `WHERE (:active=1 AND someTable.active=1)` would mean that if your parameter `:active` equals one and is True, then the second part of the clause is run (no short circuiting here).

There is nothing wrong with comparing parameters with case statements either, I do that as well, I think your issue is that all your comparisons are identical so I wouldn’t expect anything but the first case to ever come back.

Look at this thread and there’s a couple of SQL examples [Named query with many conditional WHERE clauses without query string? - #23 by kcollins1](https://forum.inductiveautomation.com/t/named-query-with-many-conditional-where-clauses-without-query-string/47880/23)

---

_[View the full topic](https://forum.inductiveautomation.com/t/sql-where-clause-case-with-multiple-values-in-then/63624)._
