PostgreSQL Dynamic unpivot

A clever dynamic solution to the unpivot problem from Thomas Kellerer:

WITH data_percentiles AS (
  SELECT
    month,
    percentile_disc(0.50) within group (order by count) "50p",
    percentile_disc(0.80) within group (order by count) "80p",
    percentile_disc(0.99) within group (order by count) "99p"
  FROM data
  GROUP BY 1
)

SELECT
  p.month,
  d.series,
  d.amount::numeric
FROM data_percentiles p
CROSS JOIN LATERAL jsonb_each_text(to_jsonb(p) - 'month') as d(series, amount)
ORDER BY 1, 2