sql - My INNER JOIN is filtering out too much -
my inner join filtering out or can't see difference is.
i trying consolidate 2 tables 2 different databases on same server.
my first step find values match:
select intl.* tbldata intl inner join [db].dbo.tbldata on intl.dataid = us.dataid , intl.anotherid = us.anotherid , intl.datavalue = us.datavalue , intl.textvalue = us.textvalue , intl.defaultvalue = us.defaultvalue , intl.ratevalue = us.ratevalue , intl.yetanotherid = us.yetanotherid i've checked columns , same type , same varchar values. query should bring matching values.
however... not case. returning 200+ records, if run query after insert 200+ records temp table:
select intl.dataid tbldata intl left join tempdatatable tdt on tdt.dataid = intl.dataid , tdt.anotherid = intl.anotherid , tdt.datavalue = intl.datavalue , tdt.textvalue = intl.textvalue , tdt.defaultvalue = intl.defaultvalue , tdt.ratevalue = intl.ratevalue , tdt.yetanotherid = intl.yetanotherid tdt.dataid null this query returns 1500+ rows.
i've run similar query data , found same discrepancy (1500+ rows returned).
i looked @ 1 record see if identify whacky, , found records each table (intl , us) same!
dataid: 1 anotherid: 1 datavalue: null textvalue: normal defaultvalue: 0 ratevalue: null yetanotherid: 1 i imagine inner join finding difference between 2 tables structurally can not see it.
any ideas on cause this?
this caused null values have in table failing equality checks of left join.
null values aren't equal anything, not null of same datatype.
to remedy this, can add check on clause null values on both sides in addition existing join conditions:
select intl.dataid tbldata intl left join tempdatatable tdt on ( (tdt.dataid null , intl.dataid null) or tdt.dataid = intl.dataid ) , ( (tdt.anotherid null , intl.anotherid null) or tdt.anotherid = intl.anotherid ) , ( (tdt.datavalue null , intl.datavalue null) or tdt.datavalue = intl.datavalue ) , ( (tdt.textvalue null , intl.textvalue null) or tdt.textvalue = intl.textvalue ) , ( (tdt.defaultvalue null , intl.defaultvalue null) or tdt.defaultvalue = intl.defaultvalue ) , ( (tdt.ratevalue null , intl.ratevalue null) or tdt.ratevalue = intl.ratevalue ) , ( (tdt.yetanotherid null , intl.yetanotherid null) or tdt.yetanotherid = intl.yetanotherid ) tdt.dataid null
Comments
Post a Comment