Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Eliminating WHERE clauses with NOT
Message
De
04/11/2014 13:46:21
Walter Meester
HoogkarspelPays-Bas
 
 
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:
01610458
Message ID:
01610487
Vues:
35
>>>Here is a sample:
>>>
>>>
>>>declare @t table (id int identity, col1 Int, col2 Int)
>>>
>>>insert into @t (col1, col2) values (1, 3), (2, 5)
>>>
>>>select * from @t where not (col1 > 3 and col2 < 6)
>>>
>>>select * from @t T where not exists (select * from @t T1 where col1 > 3 and col2 < 6 and T.id = T1.id)
>>>
>>>select * from @t where col1 <=3 or col2 >=6
>>>
>>>Normally NOT (condition1 and condition2) translates into opposite condition1 OR opposite condition2
>>
>>Yes, after second verification, I see that the execution is the same.
>>
>>I always use the NOT in such approach as it is more easy to understand. When I have something like this:
>>
>>
>>DECLARE @ModDate DateTime
>>DECLARE @NoStatus Int
>>DECLARE @ModDate2 DateTime
>>DECLARE @NoStatus2 Int
>>DECLARE @ModDate3 DateTime
>>DECLARE @NoStatus3 Int
>>
>>SET @ModDate='2013-11-09 10:31:18'
>>SET @NoStatus=5
>>SET @ModDate2='2014-11-03 10:31:18'
>>SET @NoStatus2=8
>>SET @ModDate3='2014-11-03 10:31:18'
>>SET @NoStatus3=7
>>
>>SELECT Client.Numero FROM Client (NOLOCK)
>> WHERE NOT (Client.ModDate<@ModDate AND Client.NoStatus=@NoStatus) AND
>>  NOT (Client.ModDate<@ModDate2 AND Client.NoStatus=@NoStatus2) AND
>>  NOT (Client.ModDate<@ModDate3 AND Client.NoStatus=@NoStatus3)
>>
>>
>>...it seems more easy to understand that the second one:
>>
>>
>>DECLARE @ModDate DateTime
>>DECLARE @NoStatus Int
>>DECLARE @ModDate2 DateTime
>>DECLARE @NoStatus2 Int
>>DECLARE @ModDate3 DateTime
>>DECLARE @NoStatus3 Int
>>
>>SET @ModDate='2013-11-09 10:31:18'
>>SET @NoStatus=5
>>SET @ModDate2='2014-11-03 10:31:18'
>>SET @NoStatus2=8
>>SET @ModDate3='2014-11-03 10:31:18'
>>SET @NoStatus3=7
>>
>>SELECT Client.Numero FROM Client (NOLOCK)
>> WHERE (Client.ModDate>=@ModDate OR Client.NoStatus<>@NoStatus) AND
>>  (Client.ModDate>=@ModDate2 OR Client.NoStatus<>@NoStatus2) AND
>>  (Client.ModDate>=@ModDate3 OR Client.NoStatus<>@NoStatus3)
>>
>
>This thing is still very hard to understand for me. I think the easiest here would be
>
>and Client.ModDate >=@MinDateOf3Dates OR Client.NoStatus NOT IN (@Status1, @Status2, @Status3)

That is not equivalent to the logic above. Its filtering out clients with a modification before a certain date and a certain status. It offers to pass up to three modification date - status pairs where passing a non-existing @status eliminate the inclusion of a single pair.

So,
SELECT Client.Numero FROM Client
WHERE 
    Numero NOT IN (SELECT Numero FROM Clients WHERE ModDate < @ModDate AND Status = @NoStatus) AND
    Numero NOT IN (SELECT Numero FROM Clients WHERE ModDate2 < @ModDate3 AND Status = @NoStatus2) AND
    Numero NOT IN (SELECT Numero FROM Clients WHERE ModDate2 < @ModDate3 AND Status = @NoStatus3)
Easier to understand, but not as efficient though.

Walter,
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform