SQL: retrieve only the records whose value has changed

后端 未结 7 1449
一整个雨季
一整个雨季 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:46

    I would advise, if you have control over this at this point, to only right bookended data. In other words, only write a record to the table when the rate changes. You can then assume that any data between the changes will have stayed the same. This will greatly reduce the amount of data you need to store.

    That said, this query, or something close, aught to accomplish what you're asking for:

    select rt.Code, MIN(rt.Date), rt.Rate
    from RateTable rt
    group by rt.Code, rt.Rate
    

    edit: sorry, change max to min

提交回复
热议问题