Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Simplifying WHERE clause with CASE WHEN
Message
 
 
À
21/04/2015 14:29:57
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:
01618887
Message ID:
01618901
Vues:
32
>>Why would not you post the actual UPDATE command (you can disguise column names if you want) and then it will be easier to give you the code. In any case, I don't see why the suggested approach is not going to work. You can always split one command into 2 using CTE (common table expression).
>
;with cteProvinces AS (SELECT Client.*, CASE WHEN Comp.NoCountry = 1 then Comp.NoProvince ELSE 84 END as NoProvince
from Client INNER JOIN NoCompany Comp ON Client.NoCompany = Comp.Numero
WHERE Client.FirstDate>'1899-12-30 00:00:00.000' AND
    Client.SecondDate>'1899-12-30 00:00:00.000' AND
    Client.SecondDate>=Client.FirstDate AND
    Client.NoCompany>0
),

cteTimes AS (SELECT SUM(DATEDIFF(SECOND,
    CASE WHEN BusinessTime.Start>Cl.FirstDate THEN BusinessTime.Start ELSE Cl.FirstDate END,
     CASE WHEN BusinessTime.[End]<Cl.SecondDate THEN BusinessTime.[End] ELSE Cl.SecondDate END)) AS [TotalSecondsDiff], Cl.ClientNo
     FROM BusinessTime INNER JOIN cteProvinces CL ON
      BusinessTime.NoProvince= Cl.NoProvince
      BusinessTime.NoHoliday=0 AND
      BusinessTime.Start<Cl.SecondDate AND
      BusinessTime.[End]>=Cl.FirstDate
   GROUP BY Cl.ClientNo
)

MERGE Client 
USING cteTimes T ON Client.ClientNo = T.ClientNO
WHEN Matched
SET DayBetweenFirstDateSecondDate = T.TotalSecondsDiff/3600.0/8
WHEN NOT Matched 
SET DayBetweenFirstDateSecondDate  = 0
Please verify MERGE syntax and also first try it as a select instead of MERGE to check the result. This is from the top of my head.

>
>UPDATE Client
> SET DayBetweenFirstDateSecondDate=
>  CASE
>   WHEN Client.FirstDate>'1899-12-30 00:00:00.000' AND
>    Client.SecondDate>'1899-12-30 00:00:00.000' AND
>    Client.SecondDate>=Client.FirstDate AND
>    Client.NoCompany>0 THEN
>    (SELECT COALESCE(SUM(Temp.[Second])/3600.0/8,0)
>    FROM (SELECT DATEDIFF(SECOND,
>     CASE WHEN BusinessTime.Start>Client.FirstDate THEN BusinessTime.Start ELSE Client.FirstDate END,
>     CASE WHEN BusinessTime.[End]<Client.SecondDate THEN BusinessTime.[End] ELSE Client.SecondDate END) AS [Second]
>     FROM BusinessTime
>     WHERE BusinessTime.NoProvince=
>      CASE
>       WHEN (SELECT NoCompany.NoCountry FROM NoCompany WHERE NoCompany.Numero=Client.NoCompany)=1 THEN
>       (SELECT NoCompany.NoProvince FROM NoCompany WHERE NoCompany.Numero=Client.NoCompany) ELSE 84
>      END AND
>      BusinessTime.NoHoliday=0 AND
>      BusinessTime.Start<Client.SecondDate AND
>      BusinessTime.[End]>=Client.FirstDate) Temp)
>   ELSE 0
>  END
>
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform