Skip to content

Commit b281c24

Browse files
committed
changes
1 parent ebd9db7 commit b281c24

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lead_lag.sql

+18
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ with cte as (select row_number() over() id, amount from orders)
4646
select a.id, a.amount - b.amount as rv from cte a left join cte b on a.id = b.id + 1 -- for previous add
4747
union all
4848
select a.id, a.amount - b.amount from cte a left join cte b on a.id = b.id - 1 -- for next subract
49+
50+
51+
52+
-- Example
53+
54+
with cte as (select
55+
brand,
56+
year,
57+
amount,
58+
case when amount < lead(amount, 1, amount + 1) over(partition by brand order by year) -- amount + 1 is to make sure that next value is not null
59+
then 1 else 0
60+
end as flag
61+
from brands
62+
)
63+
select
64+
brand, year, amount, flag
65+
from cte
66+
where brand not in (select brand from cte where flag= 0)

0 commit comments

Comments
 (0)