sql - Group by and Max are not working for this issue -


i have following table

id      date     indicator 12345   1/1/2001   n 12345   2/1/2001   n 12345   3/1/2001   n 12345   4/1/2001   y 12345   5/1/2001   y 12345   6/1/2001   y 67891   2/1/2002   n 67891   3/1/2002   n 67891   4/1/2002   y 67891   5/1/2002   y 67891   6/1/2002   y 

i want pull first rows indicator changes example

id      date     indicator 12345   1/1/2001   n 12345   4/1/2001   y 67891   2/1/2002   n 67891   4/1/2002   y 

and second row indicator changed

id      date     indicator 12345   4/1/2001   y 67891   4/1/2002   y 

three step solution using analytical function shown below. assume table test

-- add is_chang e= 'y' change in indicator test1 ( select a.*, case when lag(indicator,1,'x') on (partition id order date_d) != indicator 'y' end is_change test a), -- filter changes , numerate records test2 ( select a.*, row_number() on (partition id order date_d) rn test1 is_change = 'y') -- 2nd change select id, date_d, indicator test2 rn = 2 ; 

Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -