SQL - suppressing duplicate *adjacent* records

Posted by Trevel on Stack Overflow See other posts from Stack Overflow or by Trevel
Published on 2010-04-15T13:51:11Z Indexed on 2010/04/15 13:53 UTC
Read the original article Hit count: 229

Filed under:

I need to run a Select statement (DB2 SQL) that does not pull adjacent row duplicates based on a certain field. In specific, I am trying to find out when data changes, which is made difficult because it might change back to its original value.

That is to say, I have a table that vaguely resembles:

A, 5, Jan  
A, 12, Feb  
A, 12, Mar  
A, 12, Apr  
A, 9, May
A, 9, Jun  
A, 5, Jul  

And I want to get the results:

A, 5, Jan  
A, 12, Feb  
A, 9, May  
A, 5, Jul  

discarding adjacent duplicates but keeping the last row. The obvious:

Select Letter, Number, Min(Month) from Table group by Letter, Number 

does not work -- it doesn't include the last row.

© Stack Overflow or respective owner

Related posts about sql