我需要对连续整数求和并用
spoken_correctly
gt;0。
我可以通过观察找出哪些部分是连续的
lag
和
lead
,但不确定如何对连续组求和
consecutive
字段的值。
也就是说,我有两组连续的
说话正确
值>0。绿色的第一组有三行非零
说话正确
绿色的第二组有两个。
期望输出:
此SQL生成输出上方的第一个图像:
select *, case when (q.times_spoken_correctly > 0 and (q.lag > 0 or q.lead > 0)) then 1 else 0 end as consecutive
from (
select *, lag(q.times_spoken_correctly) over (partition by q.profile_id order by q.profile_id) as lag, lead(q.times_spoken_correctly) over (partition by q.profile_id order by q.profile_id) as lead
from (
SELECT *
FROM ( VALUES (3, 0, '2019-01-15 19:15:06'),
(3, 0, '2019-01-15 19:15:07'),
(3, 1, '2019-01-15 19:16:06'),
(3, 2, '2019-01-15 19:16:10'),
(3, 2, '2019-01-15 19:17:06'),
(3, 0, '2019-01-15 19:17:11'),
(3, 0, '2019-01-15 19:39:06'),
(3, 3, '2019-01-15 19:40:10'),
(3, 4, '2019-01-15 19:40:45')
) AS baz ("profile_id", "times_spoken_correctly", "w_created_at")
) as q
) as q