Используйте DISTINCT ON month, чтобы выбрать только месяцы. Затем используйте функцию управления окнами LEAD в обратном списке (через ORDER BY d DESC), чтобы получить год, предшествующий текущему году
Тест в реальном времени: http://sqlfiddle.com/#!17/5be39/2
select
distinct on ( date_part('month', (d || '-01')::date) ) -- get the months only
-- ORDER BY d DESC sort the list from most recent to oldest.
-- so LEAD here actually refers to previous year
d,
v,
lead(v) over(partition by date_part('month', (d || '-01')::date) order by d desc),
( v / lead(v) over(partition by date_part('month', (d || '-01')::date) order by d desc) )
- 1 as YoyGrowth
from progress
Выход:
| d | v | lead | yoygrowth |
|---------|-----|------|--------------------|
| 2018-06 | 100 | 90 | 0.1111111111111111 |
| 2018-07 | 105 | 92 | 0.141304347826087 |