SQL: retrieve only the records whose value has changed

后端 未结 7 1433
一整个雨季
一整个雨季 2020-12-17 02:13

Sorry for the nondescript title. I\'ll edit as we go along.

I have a table RateTable:

| Code   |  Date     |   Rate  |

  B001     2009-         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-17 02:33

    If your RDBMS supports analytic functions then the optimum method is almost certainly this:

    select code, date, rate, last_rate
    from
    (
    select code,
           date,
           rate,
           lag(rate) over (partition by code order by date) last_rate
    from   ratetable
    ) my_tb
    where  my_tb.rate != my_tb.last_rate
    

提交回复
热议问题