what is alternative of use (<> operator) when compare different values based on partid and feature name ?

ahmed salah 3,216 Reputation points
2022-06-17T18:00:34.497+00:00

I work on sql server 2017 I need to get different of feature value based on partid and feature name
I search for alternative way to compare different values based on partid and feature name without using <> not equal operator
because i face slow when using <> not equal so what i do
so i try below

create table #parts  
(  
PartId int,  
FeatureName varchar(50),  
FeatureValue varchar(50)   
)  
insert into #parts(PartId,FeatureName,FeatureValue)  
values  
(1211,'Height',50),  
(3211,'Air',90),  
(6121,'Size',300),  
(7921,'Area',790),  
(9871,'Factors',210)  
  
  
create table #partsDetails  
(  
PartId int,  
FeatureName varchar(50),  
FeatureValue varchar(50)   
)  
insert into #partsDetails(PartId,FeatureName,FeatureValue)  
values  
(1211,'Height',120),  
(3211,'Air',90),  
(6121,'Size',200),  
(7921,'Area',790),  
(9871,'Factors',410)  
  
select p.* from #parts p  
inner join #partsDetails d on p.partid=d.partid and p.featurename=d.featurename and p.featurevalue<>d.featurevalue  

212527-image.png

my question what alternative of used (<> operator) when compare different values ?

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 26,586 Reputation points
    2022-06-17T18:33:52.26+00:00

    Hi @ahmed salah ,

    Please try the following solution based on the set based EXCEPT operator.
    Sorry, this site is misbehaving. It doesn't allow me to put T-SQL as a text.

    And as @Jingyang Li already pointed out, a covering index for all three columns in question in both tables will help greatly.

    SQL

    212597-screenshot-2022-06-17-143322.png

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-06-17T18:10:34.577+00:00
     select p.* from #parts p  
      Where  exists (  
    select 1 from #partsDetails d WHERE p.partid=d.partid and p.featurename=d.featurename and p.featurevalue<>d.featurevalue)  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.