src: |
  SELECT min(p_calendrier.date) AS date,
      p_calendrier_mois.annee::text AS texte,
      min(p_calendrier.mois::text) AS alias,
      min(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_mois_comptables.mois_comptable)::text, 1, 4) || '12'::text, '000000'::text) AS max_mois,
              to_number(substr(min(p_mois_comptables.mois_comptable)::text, 1, 4) || '01'::text, '000000'::text) AS min_mois
             FROM compta.p_mois_comptables) 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_court 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 to_number(substr(max(p_mois_comptables.mois_comptable)::text, 1, 4) || '12'::text, '000000'::text) AS max_mois,
              to_number(substr(min(p_mois_comptables.mois_comptable)::text, 1, 4) || '01'::text, '000000'::text) AS min_mois
             FROM compta.p_mois_comptables) bornes
    WHERE p_calendrier.mois >= bornes.min_mois AND p_calendrier.mois <= bornes.max_mois AND p_calendrier.mois = p_calendrier_mois.mois AND substr(p_calendrier.date::text, 9, 2) = '01'::bpchar::text
  UNION ALL
   SELECT p_calendrier.date,
      p_calendrier.texte_court AS 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_mois_comptables.mois_comptable)::text, 1, 4) || '12'::text, '000000'::text) AS max_mois,
              to_number(substr(min(p_mois_comptables.mois_comptable)::text, 1, 4) || '01'::text, '000000'::text) AS min_mois
             FROM compta.p_mois_comptables) bornes
    WHERE p_calendrier.mois >= bornes.min_mois AND p_calendrier.mois <= bornes.max_mois
    ORDER BY 6 DESC, 7, 5;