WITH anchor AS (
  SELECT
    '2023-12-17' :: date AS anchor_sunday
),
series AS (
  SELECT
    (gs.gs) :: date AS period_start,
    ((gs.gs + '13 days' :: INTERVAL)) :: date AS period_end,
    ((gs.gs + '18 days' :: INTERVAL)) :: date AS payday
  FROM
    generate_series(
      (
        (
          SELECT
            anchor.anchor_sunday
          FROM
            anchor
        )
      ) :: timestamp WITH time zone,
      (((CURRENT_DATE + '1 mon' :: INTERVAL)) :: date) :: timestamp WITH time zone,
      '14 days' :: INTERVAL
    ) gs(gs)
),
numbered AS (
  SELECT
    series.period_start,
    series.period_end,
    series.payday,
    (
      EXTRACT(
        year
        FROM
          series.payday
      )
    ) :: integer AS pay_year,
    row_number() OVER (
      PARTITION BY (
        EXTRACT(
          year
          FROM
            series.payday
        )
      )
      ORDER BY
        series.payday
    ) AS pay_period_no
  FROM
    series
)
SELECT
  pay_year,
  pay_period_no,
  period_start,
  period_end,
  payday,
  (
    (
      to_char(
        (period_start) :: timestamp WITH time zone,
        'YYYY-MM-DD' :: text
      ) || '->' :: text
    ) || to_char(
      (period_end) :: timestamp WITH time zone,
      'YYYY-MM-DD' :: text
    )
  ) AS label
FROM
  numbered
WHERE
  (
    (
      payday >= ((CURRENT_DATE - '6 mons' :: INTERVAL)) :: date
    )
    AND (
      payday <= ((CURRENT_DATE + '1 mon' :: INTERVAL)) :: date
    )
  )
ORDER BY
  period_start;