Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Eliminating WHERE clauses with NOT
Message
De
05/11/2014 05:13: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:
01610458
Message ID:
01610503
Vues:
42
>>>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)


Are you sure ?

First query returns no results while the second returns one row
declare @tmp table
(	Id	int,
	LastMod	DateTime,
	Stat char(1)
	);

insert into @tmp(Id, LastMod, Stat) 
	values
	(1, '2014/01/01', 'A'),
	(2, '2014/02/01', 'A'),
	(3, '2014/03/01', 'A') ;

DECLARE @ModDate DateTime
DECLARE @NoStatus char(1)
DECLARE @ModDate2 DateTime
DECLARE @NoStatus2 char(1)
DECLARE @ModDate3 DateTime
DECLARE @NoStatus3 char(1)

SET @ModDate='2016/01/01'
SET @NoStatus='A'
SET @ModDate2='2016/01/02'
SET @NoStatus2='A'
SET @ModDate3='2014/03/01'
SET @NoStatus3='A'

select * 
	from @tmp
	WHERE (LastMod>=@ModDate OR Stat<>@NoStatus) AND
  (LastMod>=@ModDate2 OR Stat<>@NoStatus2) AND
  (LastMod>=@ModDate3 OR Stat<>@NoStatus3)
;

declare @ModDateMin DateTime;
set  @ModDateMin = @ModDate3;

select * 
	from @tmp 
	where	(LastMod >=@ModDateMin)
		 OR (Stat NOT IN (@NoStatus, @NoStatus2, @NoStatus3))
		 ;
Gregory
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform