Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Adding a CASE WHEN in a WHERE clause
Message
De
29/07/2015 16:21:59
Walter Meester
HoogkarspelPays-Bas
 
 
À
29/07/2015 15:47:36
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2012
Application:
Web
Divers
Thread ID:
01622652
Message ID:
01622658
Vues:
32
I would rewrite it into a form like
UPDATE Client
	SET DayBetweenFirstAcceptSoldDate = CASE WHEN ....WHEN ... WHEN ... THEN ... ELSE 0 END
	FROM Client
		LEFT JOIN BusinessTime ON ....
		LEFT JOIN Company ON ....
		LEFT JOIN Research ON ....
This will give the optimizer a chance to get you the best performance possible.


Walter,



>My initial select is like this:
>
>
>UPDATE Client
> SET DayBetweenFirstAcceptSoldDate=
>  CASE
>   WHEN 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 OR Client.NoResearch>0) THEN
>    (SELECT COALESCE(SUM(Temp.[Second])/3600.0/8,0)
>    FROM (SELECT DATEDIFF(SECOND,
>     CASE WHEN BusinessTime.Start>Client.FirstAcceptDate THEN BusinessTime.Start ELSE Client.FirstAcceptDate END,
>     CASE WHEN BusinessTime.[End]<Client.QueryDate THEN BusinessTime.[End] ELSE Client.QueryDate END) AS [Second]
>     FROM BusinessTime
>     WHERE BusinessTime.NoProvince=
>       (SELECT COALESCE((SELECT Company.NoProvince FROM Company WHERE Company.NoCountry=1 AND Company.Numero=Client.NoCompany),84))
>      BusinessTime.NoHoliday=0 AND
>      BusinessTime.Start<Client.QueryDate AND
>      BusinessTime.[End]>=Client.FirstAcceptDate) Temp)
>   ELSE 0
>  END
>
>
>Now, I need to condition the WHERE clause to support one way or another:
>
>
>UPDATE Client
> SET DayBetweenFirstAcceptSoldDate=
>  CASE
>   WHEN 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 OR Client.NoResearch>0) THEN
>    (SELECT COALESCE(SUM(Temp.[Second])/3600.0/8,0)
>    FROM (SELECT DATEDIFF(SECOND,
>     CASE WHEN BusinessTime.Start>Client.FirstAcceptDate THEN BusinessTime.Start ELSE Client.FirstAcceptDate END,
>     CASE WHEN BusinessTime.[End]<Client.QueryDate THEN BusinessTime.[End] ELSE Client.QueryDate END) AS [Second]
>     FROM BusinessTime
>     WHERE BusinessTime.NoProvince=
>      CASE
>       WHEN Client.NoCompany>0 THEN
>        (SELECT COALESCE((SELECT Company.NoProvince FROM Company WHERE Company.NoCountry=1 AND Company.Numero=Client.NoCompany),84))
>       ELSE
>        (SELECT COALESCE((SELECT Research.NoProvince FROM Research WHERE Research.NoCountry=1 AND Research.Numero=Client.NoResearch),84))
>       END AND 
>      BusinessTime.NoHoliday=0 AND
>      BusinessTime.Start<Client.QueryDate AND
>      BusinessTime.[End]>=Client.FirstAcceptDate) Temp)
>   ELSE 0
>  END
>
>
>However, this is having some weird results on SQL Server. I can execute that command, which used to run for about 3 minutes, and now it can takes up to 24 hours and still not finished.
>
>The Client table has 3 million records and all related indexes are there.
>
>I assume the syntax I am using is not a proper approach.
>
>Anyone would know what it could be?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform