A bike-sharing network collects a fare whenever a rider begins a trip. Its planning team wants to compare each station's annual fare revenue with its previous recorded year.
payment_id is the primary key. Each row records one fare paid for a trip that started at a station.
For every station and calendar year, calculate the total fare_amount. Then compare that total with the same station's previous recorded year, meaning the preceding row when that station's yearly totals are ordered by year. If a station has no activity in an intervening calendar year, compare with its most recent earlier year that does have data.
Return these columns:
year
station_id
curr_year_revenue, rounded to 2 decimal places
prev_year_revenue, rounded to 2 decimal places
yoy_rate, calculated as (curr_year_revenue - prev_year_revenue) / prev_year_revenue * 100 and rounded to 2 decimal places
For a station's first recorded year, return NULL for both prev_year_revenue and yoy_rate. If the previous year's revenue is 0, return NULL for yoy_rate.
Order the results by station_id ascending, then year ascending.
The two payments from 2023 total 100.00, so that becomes station 101's first annual value. Revenue rises from 100.00 to 150.00 in 2024, a 50.00% increase. It then falls from 150.00 to 120.00 in 2025, a -20.00% change.