How can I select rows that do not match corresponding rows in the same table?

10,433

Solution 1

Assuming the following:
1. The values are fixed (i.e. 1.0 to 1.1 and 2.0 to 2.1)
2. What you're looking for is the original row where the corresponding row does not exist
3. The join between original and corresponding rows is in the columns you haven't mentioned (I've called it key below)
... then something like the following should work:

select original.*
from table original
  left outer join table corresponding on corresponding.key = original.key and corresponding.Col_A = 'val1.1' and corresponding.Col_B = 'val2.1'
where original.Col_A = 'val1.0'
  and original.Col_B = 'val2.0'
  and corresponding.key is null

Solution 2

Select e.*  from table e
where (e.Col_A = val1.0 and e.Col_B = val2.0) 
and NOT EXISTS(SELECT * FROM table e2 
           WHERE e2.Col_A = val1.1 
           and e2.Col_B = val2.1 AND e2.key = e.key) 

Solution 3

I'm assuming that your values 1.0 and 1.1 are just one example of many. Otherwise there would not be multiple records to look for. Are you looking for values where A2 = (A1 + 0.1)? If so, you could use something like this:

select t1.col_a, t1.col_b
from table t1
left join table t2 on 
    (t2.col_a = t1.col_a + 0.1 and t2.col_b = t1.col_b + 0.1)
where
    t2.col_a is null and t2.col_b is null

The LEFT JOIN will return null values when there is no record that meets the join criteria.

Share:
10,433
dee
Author by

dee

Updated on June 05, 2022

Comments

  • dee
    dee almost 2 years

    I'm trying to figure out how to work out this query.

    I have a table with multiple columns. Of these columns there are two: colA and colB.

    The values in colA and colB in a row will match a different row in the same table, however the actual values in those columns will not be equal.

    So for any row:

    Col_A | Col_B
    val1.0  | val2.0
    

    there will be another row

    Col_A | Col_B
    val1.1| val2.1
    

    There is an exception where this corresponding row does not exist and it is this row that I need to select.

    Any help with the logic around this is greatly appreciated.

    I've been working with queries of this type

    Select * 
    from table1 e 
    where (e.Col_A = val1.0 and e.Col_B = val2.0)
    and (e.Col_A != val1.1 and e.Col_B ! = val2.1)
    

    is this even close? The above strikes me as I write it that if one clause is true then the other must also be true.

    Thanks in advance for any help or hints.

    Additional info as there were requests for clarification.

    What I'm doing is updating a table. This table has a column that will catch and mark any exceptions to the rule I stated above.

    The table has about 12 columns and two of these columns can have one of two possible values. Value Types for these two columns are varchar2(30) Normally for any row that has a set of values there will be another row that has the other values. Some of the other unrelated columns can be the same but not necessarily.

    The code I have implemented is similar to below and is based on the left join suggestion from KAJ. However it seems to return too many rows in a straight query and in the update it actually updates every row.

    Update TableA Set Col_Ex = ('Exception')
    Where Exists 
    (Select ... Example from Kaj s suggestion below);