Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Compare one column between two tables

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-09-12 : 14:47:48
I have these two select statements that are pulling back the same number or rows from these two tables. This IS what i expected.


Select * from offense.dbo.OFF_PRP where OFFENSENO like '%824744' order by Uniquekey
Select * from Douglasconversion.dbo.propertysheetdata where CFSID = '824744' order by Uniquekey


I'm being asked to verify the data further by showing, via an inner join, that the column named "Quantity" in both tables has the same value when compared against the Uniquekey value in both tables. What i should be showing is a resultset with nothing in it when i ask for any columns between the two tables where Quantity<>Quantity.

Below is what i'm using, but i'm not getting very far:


Select a.* from offense.dbo.OFF_PRP a
where a.QUANTITY<>b.Quantity
Inner Join Douglasconversion.dbo.propertysheetdata b
On a.uniquekey = b.uniquekey

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-12 : 14:50:00
syntax is wrong
it should be

Select a.* from offense.dbo.OFF_PRP a
Inner Join Douglasconversion.dbo.propertysheetdata b
On a.uniquekey = b.uniquekey
where a.QUANTITY<>b.Quantity


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

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-12 : 15:07:54
If the quantity columns are nullable, you would also want to add something to the effect of what I have shown below in red:
Select a.* from offense.dbo.OFF_PRP a
Inner Join Douglasconversion.dbo.propertysheetdata b
On a.uniquekey = b.uniquekey
where (a.QUANTITY<>b.Quantity)
OR
(a.Quantity IS NULL AND b.Quantity IS NOT NULL)
OR
(a.Quantity IS NOT NULL AND b.Quantity IS NULL)

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-12 : 15:32:07
where COALESCE(a.QUANTITY,0)<>COALESCE(b.Quantity,0)?


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

Go to Top of Page
   

- Advertisement -