SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Comparing data strings using "Where not exists"
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nelsont
Starting Member

USA
25 Posts

Posted - 01/09/2013 :  19:47:01  Show Profile  Reply with Quote
A while ago I posted in here because I was doing an insert into a table based on using "Not In" to see if the data already existed in the target table. I kept getting the same results in my query when I switched the "Not In" to "In" because of the way SQL handles nulls. It was suggested that I use "not exists" or "exists" instead.

I am trying to get this to work and I get the same results whether I use = or <>. None of these fields are nullable. The reason I am casting them all as varchar is because they are all int and the total of the three fields could be the same even if the individual values are different.


This is driving me crazy. The 'where not exists' has worked fantastically in all of the other parts of my SSIS routine, but none of those are using combinations of fields that are all int - they are a mix of data types.

Any ideas?


insert into tableA (
   field1,
   field2,
   field3)
select 
   b.field1,
   c.field2,
   d.field3
from tableB b
   left join tableC c on b.id = c.id
   left join tableD d on b.id = d.id
   where not exists(
    select null from tableA where
       cast(field1 as varchar(20))+
       cast(field2 as varchar(20))+
       cast(field3 as varchar(20)))
     =
       cast(b.field1 as varchar(20))+
       cast(b.field2 as varchar(20))+
       cast(b.field3 as varchar(20)))
)





I have also tried hashing each group of fields on either side of the = sign to no avail. I'm just stuck.
Any help would be greatly appreciated.

LoztInSpace
Aged Yak Warrior

938 Posts

Posted - 01/09/2013 :  20:18:10  Show Profile  Reply with Quote
Why not LEFT OUTER JOIN tableA on b.field1=tableA.field1 and b.field2=tableA.field2 and b.field3=tableA.field3 WHERE tableA.field1 IS NULL ? Much nicer than your concatenation kludge.
Go to Top of Page

nelsont
Starting Member

USA
25 Posts

Posted - 01/09/2013 :  21:58:07  Show Profile  Reply with Quote
do you mean instead of what I have in the not exists clause? where should your code go in my insert statement?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/09/2013 :  22:41:32  Show Profile  Reply with Quote
quote:
Originally posted by LoztInSpace

Why not LEFT OUTER JOIN tableA on b.field1=tableA.field1 and b.field2=tableA.field2 and b.field3=tableA.field3 WHERE tableA.field1 IS NULL ? Much nicer than your concatenation kludge.



i dont think that will work in this case as OPs requirement states emphasis is on concatenated result and not on individual values. See below statement

The reason I am casting them all as varchar is because they are all int and the total of the three fields could be the same even if the individual values are different.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

LoztInSpace
Aged Yak Warrior

938 Posts

Posted - 01/10/2013 :  00:33:37  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

quote:
Originally posted by LoztInSpace

Why not LEFT OUTER JOIN tableA on b.field1=tableA.field1 and b.field2=tableA.field2 and b.field3=tableA.field3 WHERE tableA.field1 IS NULL ? Much nicer than your concatenation kludge.



i dont think that will work in this case as OPs requirement states emphasis is on concatenated result and not on individual values. See below statement

The reason I am casting them all as varchar is because they are all int and the total of the three fields could be the same even if the individual values are different.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




I interpreted that to mean "I can only use EXISTS on one column but my condition is 3 columns so I stuck them together. As they are numbers, a mathematical sum would not be sufficiently unique".
Of course if 10, 11 & 12 should match 101, 1 and 12 then I am wrong.
However, assuming my interpretation:
quote:
Originally posted by nelsont
do you mean instead of what I have in the not exists clause? where should your code go in my insert statement?


Yes. It needs to go in the select:


...
from tableB b
   left join tableC c on b.id = c.id
   left join tableD d on b.id = d.id
   where not exists(
    select null from tableA where
       cast(field1 as varchar(20))+
       cast(field2 as varchar(20))+ 
    LEFT OUTER JOIN tableA on 
          b.field1=tableA.field1 and 
          b.field2=tableA.field2 and 
          b.field3=tableA.field3 
    WHERE tableA.field1 IS NULL


If visakh16's interpretation is right then just join on the concatenated strings:

    LEFT OUTER JOIN tableA on 
          b.field1+b.field2+b.field2 =
          tableA.field1+tableA.field2+tableA.field2 
    WHERE tableA.field1 IS NULL

(with casts but I'm too lazy to type all that in ).
Bear in mind the concatenation will preclude the use of indexes so you'll get a different plan that may be slower.
Go to Top of Page

nelsont
Starting Member

USA
25 Posts

Posted - 01/10/2013 :  13:11:22  Show Profile  Reply with Quote
The left outer join worked. Thank you for the help. The problem is that I don't understand why.

I thought that the way I was doing it was sound, just not preferred. Is there something inherently wrong with the way I had it set up?
Go to Top of Page

jamesbrummel
Starting Member

5 Posts

Posted - 01/10/2013 :  14:08:04  Show Profile  Reply with Quote
NOT or <> etc tend to be pretty slow, better to find your matches first, what's left is the NOTs, which is what a LEFT join does.

Most people who drown do so in chest deep water.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/10/2013 :  22:50:27  Show Profile  Reply with Quote
quote:
Originally posted by nelsont

The left outer join worked. Thank you for the help. The problem is that I don't understand why.

I thought that the way I was doing it was sound, just not preferred. Is there something inherently wrong with the way I had it set up?


So was your requirement to match on individual column names? your initial explanation sounded as if you're interested in concatenated result

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

nelsont
Starting Member

USA
25 Posts

Posted - 01/11/2013 :  04:22:48  Show Profile  Reply with Quote
It was to match on individual column names, but since they were all int, if I just did col1+col2+col2 = b.col1+b.col2+b.col3 then sql would actually do the math and match the totals.

Since the individual column values could be different but the total of all three could be the same, that could give me incorrect results. That's why I casted them all as varchar, so I could compare the strings.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/11/2013 :  06:04:49  Show Profile  Reply with Quote
quote:
Originally posted by nelsont

It was to match on individual column names, but since they were all int, if I just did col1+col2+col2 = b.col1+b.col2+b.col3 then sql would actually do the math and match the totals.

Since the individual column values could be different but the total of all three could be the same, that could give me incorrect results. That's why I casted them all as varchar, so I could compare the strings.


ok..that makes it clear

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

LoztInSpace
Aged Yak Warrior

938 Posts

Posted - 01/11/2013 :  10:02:21  Show Profile  Reply with Quote
quote:
Originally posted by nelsont

It was to match on individual column names, but since they were all int, if I just did col1+col2+col2 = b.col1+b.col2+b.col3 then sql would actually do the math and match the totals.

Since the individual column values could be different but the total of all three could be the same, that could give me incorrect results. That's why I casted them all as varchar, so I could compare the strings.


The same can be said for the strings:
10,20,30 also matches
10,203,0
or
1020,3,0
so watch out for these kludges.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000