[英]How to select rows where a value has changed, an item is new for a date or leaves from previous date
[英]How to get dates where value changed in rows of date/value tuples?
我有一個表(實際上是一個子查詢結果),其元組為DATE
和NUMBER
。 日期是無間隙的,並且在給定的時間范圍內排序。
countdate | countvalue
-----------------------
2014-01-01 | 5
2014-01-02 | 5
2014-01-03 | 5
2014-01-04 | 4
2014-01-05 | 4
2014-01-06 | 4
2014-01-07 | 5
如何選擇與前一天相比計數值發生變化的所有計數日期(具有相應的計數值)?
ANSI標准lag()
函數可以滿足您的要求:
select s.countdate, s.countvalue
from (select s.*, lag(countvalue) over (order by countdate) as prevvalue
from (<subquery>) s
) s
where prevvalue is null or prevvalue <> countvalue;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.