我遇到这种情况:2列
score date
-1 09/02/2018
-1 08/02/2018
-1 07/02/2018
2 06/02/2018
2 05/02/2018
2 04/02/2018
-1 02/02/2018
-1 01/02/2018
如何获取最后一次得分为-1
的最短日期?
我需要获取日期:07/02/2018
答案 0 :(得分:0)
您可以使用以下方式执行此操作:
select min(t.date)
from t
where t.score = -1 and
t.date > (select max(t2.date) from t t2 where t2.score <> -1);
答案 1 :(得分:0)
这将返回数据中最新系列-1的最早日期:
SELECT Max(dt)
FROM
( -- find all the rows starting a new series of -1
SELECT datecol as dt,
Min(score) -- score of previous date
Over (ORDER BY datecol DESC
ROWS BETWEEN 1 Following AND 1 Following) AS prev_score
FROM tab
QUALIFY score = -1 -- current score must be -1
AND (prev_score <> -1 OR prev_score IS NULL) -- and previous score either <> -1 or NULL (data starts with -1)
) AS dt
如果您有多个组,则可以添加PARTITION BY和GROUP BY。