Avatar of svs1919
svs1919
 asked on

un matching records in tables


The code below extracts the un matching records from c and b
i have to change code so that c.street1 should compare with b.address2 and rest as it is
2nd thing for some records address ,zip might not match but if state matches the data is not extracted for example
Con Table                        Block table
street1  Street2  state   zip          Address1 Address2   state   zip
xyz            nj     08815              Abc          nj     03310


in this case record is not extracted bcos NJ matches in both tables thats not what i was expecting.if any of these columns will not match i should get the record
select
      *
From
      New n
      join Page p On N.ITEM = P.ITEM
      join dbo.Lan l On n.onumber = l.onumber
      Join Con c On c.oId = l.Id
      inner join (
                  select
                        onumber,
                        count(*) as quantity
                  from
                        dbo.Lan
                  group by
                        oNumber
                 ) q on n.ONUMBER = q.onumber
      Left Outer Join block b (NOLOCK) On N.DEALER_ID = b.CUSTNMBR
Where
      n.ORDER_QUANTITY = q.quantity
and	b.address1 is null and  b.ADDRESS2 is null and  b.city is null and b.state is null and b.zip is null

Open in new window

Microsoft SQL Server 2005Microsoft SQL Server

Avatar of undefined
Last Comment
jaryco

8/22/2022 - Mon
chapmandew

I believe I worked w/ you on this earlier in the week...you're not setting any criteria on the fields other than making sure they are not null..don't you think you shoudl add some more criteria to the address fields?
svs1919

ASKER
ya i asked a question similar to this but that was for matching records. This is for unmatching records and now the problen is i cant say c.street1 <> b.address2 bcos this in a subquery. and if even 1 column matches i am not getting the records. Can you help me out in this
jaryco

You aren't getting those records because you just set criteria to avoid rows with NULL in Address1, Address2, state and zip fields.

Don't you think should be better try something like this:
AND (c.street1 <> b.address1 OR c.street2 <> b.address2 OR ...)
Your help has saved me hundreds of hours of internet surfing.
fblack61
svs1919

ASKER
i will try this but can we use <> in a sub query as some one told me thats  not a right way to do that
svs1919

ASKER
Ad also there are chances of having multiple addresses for 1 record  so we just have to compare with
the matching record
ASKER CERTIFIED SOLUTION
jaryco

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.