Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Eliminating WHERE clauses with NOT
Message
De
04/11/2014 16:46:29
Metin Emre
Ozcom Bilgisayar Ltd.
Istanbul, Turquie
 
 
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:
01610492
Vues:
29
>>>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)

get @nostatus = 1 and @moddate = 1-1-2014
NOT (Client.NoStatus=@NoStatus AND Client.ModDate<@ModDate)
you want to records other than nostatus value = 1 and moddate value = 1-1-2014 in this case
NOT (Client.NoStatus=1 AND Client.ModDate<1-1-2014)
so;
(Client.NoStatus<>@NoStatus OR Client.ModDate>=@ModDate)
gives you nostatus value # 1 or client.moddate >= 1-1-2014 if any of two case is true, you won't get records nostatus = 1 and moddate=1-1-2014. put values:
(Client.NoStatus<>1 OR Client.ModDate>=1-1-2014)
I think it's more easy with put values. When I optimise my queries every time I think with put values. :)
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform