Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding a CASE WHEN in a WHERE clause
Message
 
 
To
29/07/2015 16:14:21
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2012
Application:
Web
Miscellaneous
Thread ID:
01622652
Message ID:
01622664
Views:
38
>>I suggest to re-write this statement.
>>
>>1. Start from a select statement to return what you will need.
>>
>>2. Once you get your select statement working properly, turn it into MERGE command.
>
>The original select was what I needed. I only needed to add a condition in the WHERE clause to use one way or another based on the fact that if there is a company or not. The default, is to use the province from the Company table, if we have a Client.NoCompany. Otherwise, we use the province from the Research table, if we have Client.NoCompany=0 and a Client.NoResearch>0.

Start from this query:
;with cte as (select Client.*, COALESCE(Company.NoProvince, 84) as NoProvince 
from Client LEFT JOIN Company ON Client.NoCompany = Company.Numero and Company.NoCountry = 1
WHERE Client.FirstAcceptDate>'1899-12-30 00:00:00.000' AND
    Client.QueryDate>'1899-12-30 00:00:00.000' AND
    Client.QueryDate>=Client.FirstAcceptDate AND
    Client.NoCompany>0
UNION ALL
select Client.*, COALESCE(Research.NoProvince, 84) as NoProvince 
from Client LEFT JOIN Research ON Client.NoReseach = Research.Numero and Research.NoCountry = 1
WHERE Client.FirstAcceptDate>'1899-12-30 00:00:00.000' AND
    Client.QueryDate>'1899-12-30 00:00:00.000' AND
    Client.QueryDate>=Client.FirstAcceptDate AND
    Client.NoResearch>0)

select * from cte
This is what you're going to join with the sum of the BusinessTime.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform