Search code examples
sqlsql-serversql-server-2014

Remove Adjustment Rows SQL Select Statment


I would like to remove adjustment rows from my SQL select query. Here is what I have right now:

Resource    Date    Leave       Position    Hours   Code
33333   26/02/2016  Sick Leave  TRNSPLNR    -7      SICK                     
33333   26/02/2016  Sick Leave  TRNSPLNR     7      SICK                     
33333   26/02/2016  Vacation    TRNSPLNR     7      VAC 

This is what the end result should be:

Resource Date       Leave     Position   Hours  Code                   
33333    26/02/2016 Vacation  TRNSPLNR    7     VAC 

Solution

  • Assuming that there are exactly 2 such duplicates, you can do:

    select t.*
    from t
    where not exists (select 1
                      from t t2
                      where t2.date = t.date and 
                            t2.leave = t.leave and
                            t2.resource = t.resource and
                            t2.hours = - t.hours
                     );
    

    It is not clear what makes a duplicate, so you might want to add more comparisons into the inner where clause.