1

I am trying to setup a conditional statement that will determine which WHERE clause to use.

I need to check 2 separate conditions, one on each side of the AND and if results are greater than 1 then use a particular statement if not then use nothing

Like this logic but in pdw sql

WHERE 
if cte1.counte > 1 then 'cte1.built is not null' else '' 
AND 
if cte2.countd > 1 then 'cte2.demo is not null' else ''

possible combinations:

WHERE CTE1.BUILD IS NOT NULL
WHERE CTE1.BUILD IS NOT NULL AND CTE2.DEMO IS NOT NULL
WHERE CTE2.DEMO IS NOT NULL
BLANK

Is this possible to do?

Thanks in advance

Josephff
  • 99
  • 1
  • 9
  • Are you familiar with CASE? Here is a relevant question: [“CASE” statement within “WHERE” clause in SQL Server 2008](https://stackoverflow.com/questions/8785209/case-statement-within-where-clause-in-sql-server-2008) – Abra Apr 30 '19 at 17:48
  • yes, I've looked up hundreds of case threads but I am not exchanging a field value, I need to exchange the actual where statement itself and I didn't find one that was similar to that – Josephff Apr 30 '19 at 18:00

1 Answers1

2

Something like this:

WHERE (cte1.counte > 1 and cte1.built is not null or cte1.counte <= 1) and
      (cte2.countd > 1 and cte2.demo is not null or cte2.countd <= 1)
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786