Friday, January 21, 2011

How to get combined columns NOT IN another table

Lets say we have two tables:

  1. TableA
    1. item_no
    2. upc
  2. TableB
    1. item_no
    2. upc
One item_no can have one or more upcs
On the TableA we have 150 records, on the TableB we have 100 records.
We want to know which items with its upc doesn't exist on the TableB.
The easiest way to solve the problem is join the two columns to manipulate them as one:
   -- Example to find rows not in other table

   SELECT TableA.item_no + '@' + TableA.upc 
       FROM TableA
       WHERE TableA.item_no + '@' + TableA.upc NOT IN (
                select TableB.item_no + '@' + TableB.upc FROM TableB
              )


No comments:

Post a Comment