src: |
  SELECT p_calendrier.date,
      p_calendrier_mois.texte ||
          CASE
              WHEN p_calendrier_mois.mois = bornes.max_mois_mat2a THEN ' - Dernier mois e-PMSI'::text
              WHEN p_calendrier_mois.mois = bornes.max_mois_fildeleau THEN ' - Mois en cours'::text
              ELSE ''::text
          END AS texte,
      p_calendrier.mois::text AS alias,
      p_calendrier.mois,
      '1'::text AS level,
      209901::numeric(6,0) AS tri1,
      '1962-04-18'::date AS tri2
     FROM base.p_calendrier,
      base.p_calendrier_mois,
      ( SELECT max(
                  CASE
                      WHEN p_chiffrier.nb_rsa_champ_ghs <> 0::numeric THEN p_chiffrier.mois
                      ELSE 0::numeric
                  END) AS max_mois_mat2a,
              max(p_chiffrier.mois) AS max_mois_fildeleau,
              to_number(substr(max(p_chiffrier.mois)::text, 1, 4) || '12'::text, '000000'::text) AS max_mois,
              to_number(substr(min(p_chiffrier.mois)::text, 1, 4) || '01'::text, '000000'::text) AS min_mois
             FROM pmsi.p_chiffrier) bornes
    WHERE p_calendrier.mois = bornes.max_mois_mat2a AND p_calendrier.mois = p_calendrier_mois.mois AND p_calendrier.dernier_jour = '1'::bpchar
  UNION ALL
   SELECT p_calendrier.date,
      p_calendrier_mois.texte ||
          CASE
              WHEN p_calendrier_mois.mois = bornes.max_mois_mat2a THEN ' - Dernier mois e-PMSI'::text
              WHEN p_calendrier_mois.mois = bornes.max_mois_fildeleau THEN ' - Mois en cours'::text
              ELSE ''::text
          END AS texte,
      p_calendrier.mois::text AS alias,
      p_calendrier.mois,
      '1'::text AS level,
      209912::numeric(6,0) AS tri1,
      '1962-04-18'::date AS tri2
     FROM base.p_calendrier,
      base.p_calendrier_mois,
      ( SELECT max(
                  CASE
                      WHEN p_chiffrier.nb_rsa_champ_ghs <> 0::numeric THEN p_chiffrier.mois
                      ELSE 0::numeric
                  END) AS max_mois_mat2a,
              max(p_chiffrier.mois) AS max_mois_fildeleau,
              to_number(substr(max(p_chiffrier.mois)::text, 1, 4) || '12'::text, '000000'::text) AS max_mois,
              to_number(substr(min(p_chiffrier.mois)::text, 1, 4) || '01'::text, '000000'::text) AS min_mois
             FROM pmsi.p_chiffrier) bornes
    WHERE p_calendrier.mois = bornes.max_mois_fildeleau AND p_calendrier.mois <> bornes.max_mois_mat2a AND p_calendrier.mois = p_calendrier_mois.mois AND p_calendrier.dernier_jour = '1'::bpchar
  UNION ALL
   SELECT max(p_calendrier.date) AS date,
      p_calendrier_mois.annee::text AS texte,
      max(p_calendrier.mois)::text AS alias,
      max(p_calendrier.mois)::numeric(6,0) AS mois,
      '1'::text AS level,
      max(p_calendrier.mois)::numeric(6,0) AS tri1,
      '1962-04-18'::date AS tri2
     FROM base.p_calendrier,
      base.p_calendrier_mois,
      ( SELECT to_number(substr(max(p_chiffrier.mois)::text, 1, 4) || '12'::text, '000000'::text) AS max_mois,
              to_number(substr(min(p_chiffrier.mois)::text, 1, 4) || '01'::text, '000000'::text) AS min_mois
             FROM pmsi.p_chiffrier) bornes
    WHERE p_calendrier.mois >= bornes.min_mois AND p_calendrier.mois <= bornes.max_mois AND p_calendrier.mois = p_calendrier_mois.mois
    GROUP BY p_calendrier_mois.annee
  UNION ALL
   SELECT p_calendrier.date,
      p_calendrier_mois.texte ||
          CASE
              WHEN p_calendrier_mois.mois = bornes.max_mois_mat2a THEN ' - Dernier mois e-PMSI'::text
              WHEN p_calendrier_mois.mois = bornes.max_mois_fildeleau THEN ' - Mois en cours'::text
              ELSE ''::text
          END AS texte,
      p_calendrier.mois::text AS alias,
      p_calendrier.mois,
      '2'::text AS level,
      p_calendrier.mois AS tri1,
      '1962-04-18'::date AS tri2
     FROM base.p_calendrier,
      base.p_calendrier_mois,
      ( SELECT max(
                  CASE
                      WHEN p_chiffrier.nb_rsa_champ_ghs <> 0::numeric THEN p_chiffrier.mois
                      ELSE 0::numeric
                  END) AS max_mois_mat2a,
              max(p_chiffrier.mois) AS max_mois_fildeleau,
              to_number(substr(max(p_chiffrier.mois)::text, 1, 4) || '12'::text, '000000'::text) AS max_mois,
              to_number(substr(min(p_chiffrier.mois)::text, 1, 4) || '01'::text, '000000'::text) AS min_mois
             FROM pmsi.p_chiffrier) bornes
    WHERE p_calendrier.mois >= bornes.min_mois AND p_calendrier.mois <= bornes.max_mois AND p_calendrier.mois = p_calendrier_mois.mois AND p_calendrier.dernier_jour = '1'::bpchar
  UNION ALL
   SELECT p_calendrier.date,
      p_calendrier.texte,
      ''::text AS alias,
      p_calendrier.mois,
      '3'::text AS level,
      p_calendrier.mois AS tri1,
      p_calendrier.date AS tri2
     FROM base.p_calendrier,
      ( SELECT to_number(substr(max(p_chiffrier.mois)::text, 1, 4) || '12'::text, '000000'::text) AS max_mois,
              to_number(substr(min(p_chiffrier.mois)::text, 1, 4) || '01'::text, '000000'::text) AS min_mois
             FROM pmsi.p_chiffrier) bornes
    WHERE p_calendrier.mois >= bornes.min_mois AND p_calendrier.mois <= bornes.max_mois
    ORDER BY 6 DESC, 7, 5;