targo-backend/prisma/migrations/20250724191659_create_pay_period_view/migration.sql

47 lines
1.2 KiB
SQL

-- This is an empty migration.
-- Date d'ancrage fix. Période 1 = 2023-12-17 => 2023-12-30
WITH anchor AS (
SELECT '2023-12-17'::date AS anchor_date
),
--definition des bornes d'archivage et de visionnement futur ( 6 mois avant la periode actuelle et 1 mois apres la periode actuelle)
With bounds As (
SELECT
(now()::date - INTERVAL ' 6 months')::date AS start_bound,
(now()::date - INTERVAL ' 1 month')::date AS end_bound,
anchor.anchor_date
FROM anchor
),
--determination des dates d'une periode
series AS (
SELECT
generate_series(
bounds.start_bound,
bounds.end_bound,
'14 days'
) AS period_start
bounds.anchor_date
FROM bounds
)
--creation de la vue
CREATE OR REPACE VIEW "pay_period" AS
SELECT
((row_number() OVER (ORDER BY period_start) - 1) % 26) + 1
AS period_number,
period_start AS start_date, --date de début de la periode
period_start + INTERVAL '13 days' AS end_date, -- date de fin de la periode
EXTRACT(YEAR FROM period_start)::int AS year,
--label pour affichage
|| ''
|| to_char(period_start + INTERVAL '13 days', 'YYYY-MM-DD')
AS label
FROM series;
OERDER BY period_start;