sql server 2012 - Conditional Row Deleting in SQL -


i have table contains 4 columns. need remove of rows based on code , id columns. code of 1 initiates process i'm trying track , code of 2 terminates it. remove rows specific id when code of 2 comes after code of 1 , there not additional code 1. example, current data set looks this:

code  deposit    date        id 1      $100      3/2/2016    5 2      $0        3/1/2016    5 1      $120      2/8/2016    5 1      $120      3/22/2016   4 2      $70       2/8/2016    3 1      $120      1/3/2016    3 2      $0        6/15/2015   2 1      $120      3/22/2016   2 1      $50       8/15/2015   1 2      $200      8/1/2015    1 

after run script this:

code  deposit    date        id 1      $100      3/2/2016    5 2      $0        3/1/2016    5 1      $120      2/8/2016    5 1      $120      3/22/2016   4 1      $50       8/15/2015   1 2      $200      8/1/2015    1 

in have 150,000 id's in actual table general idea.

you can ids using logic this:

select t.id t group t.id having max(case when code = 2 date end) > min(case when code = 1 date end) , -- code 2 after code 1        max(case when code = 2 date end) > max(case when code = 1 date end) -- no code 1 after code2 

it easy enough incorporate query rest of details:

select t.* t t.id not in (select t.id                    t                    group t.id                    having max(case when code = 2 date end) > min(case when code = 1 date end) , -- code 2 after code 1                           max(case when code = 2 date end) > max(case when code = 1 date end)                   ); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -