Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 7.72 KB | None | 0 0
  1. SELECT
  2.   t2.code_dr,
  3.   t2.libelle_dr,
  4.   t2.libelle_contrat,
  5.   SUM(t2.somme_echeance_calculee) AS montant,
  6.   SUM(t2.nb_echeance_calculee) AS nb
  7. FROM
  8.   (
  9.     SELECT
  10.       t.num_version_contrat,
  11.       t.libelle_contrat,
  12.       t.libelle_type_contrat,
  13.       t.libelle_etat_contrat,
  14.       t.dernier_montant,
  15.       t.libelle_dr,
  16.       t.code_dr,
  17.       t.flag_pris_en_compte_dans_V3_V2,
  18.       t.date_effet_contrat,
  19.       t.date_fin_contrat,
  20.       GROUP_CONCAT(DISTINCT t.code_nidt SEPARATOR '\n') code_nidt,
  21.       t.date_debut_periode_echeance,
  22.       t.date_fin_periode_echeance,
  23.       (
  24.         CASE WHEN t.date_fin_contrat > DATE('2019-12-31') THEN FLOOR(
  25.           TIMESTAMPDIFF(
  26.             MONTH,
  27.             t.date_quittancement_echeance2,
  28.             DATE('2019-12-31')
  29.           ) / 12
  30.         ) ELSE FLOOR(
  31.           TIMESTAMPDIFF(
  32.             MONTH,
  33.             t.date_quittancement_echeance2,
  34.             t.date_fin_contrat
  35.           ) / 12
  36.         ) END
  37.       ) nb_echeance_calculee,
  38.       (
  39.         CASE WHEN t.date_fin_contrat > DATE('2019-12-31') THEN t.montant_ht_actualise_echeance * (
  40.           (
  41.             1 - (
  42.               pow(
  43.                 (1 + 0.02),(
  44.                   FLOOR(
  45.                     TIMESTAMPDIFF(
  46.                       MONTH,
  47.                       t.date_quittancement_echeance2,
  48.                       DATE('2019-12-31')
  49.                     ) / 12
  50.                   )
  51.                 )
  52.               )
  53.             )
  54.           ) / (1 -(1 + 0.02))
  55.         ) ELSE t.montant_ht_actualise_echeance * (
  56.           (
  57.             1 - (
  58.               pow(
  59.                 (1 + 0.02),(
  60.                   FLOOR(
  61.                     TIMESTAMPDIFF(
  62.                       MONTH,
  63.                       t.date_quittancement_echeance2,
  64.                       t.date_fin_contrat
  65.                     ) / 12
  66.                   )
  67.                 )
  68.               )
  69.             )
  70.           ) / (1 -(1 + 0.02))
  71.         ) END
  72.       ) somme_echeance_calculee,
  73.       (
  74.         CASE WHEN t.code_indice LIKE '%\%%' THEN ROUND(
  75.           (
  76.             t.montant_ht_actualise_echeance *(
  77.               1 +(
  78.                 CAST(
  79.                   REPLACE(
  80.                     (
  81.                       SUBSTRING(t.code_indice, 1, LOCATE('%', t.code_indice) -1)
  82.                     ),
  83.                     ',',
  84.                     '.'
  85.                   ) AS DECIMAL(10, 4)
  86.                 )
  87.               ) / 100
  88.             )
  89.           ),
  90.           2
  91.         ) WHEN t.code_indice LIKE 'ICC%' THEN CASE WHEN REPLACE(
  92.           t.periode_courante_indice_echeance,
  93.           SUBSTRING(t.periode_courante_indice_echeance, 1, 4),(
  94.             CAST(
  95.               SUBSTRING(t.periode_courante_indice_echeance, 1, 4) AS SIGNED INTEGER
  96.             ) + 1
  97.           )
  98.         ) IN (
  99.           SELECT
  100.             icc_periode
  101.           FROM
  102.             indice_icc
  103.         ) THEN ROUND(
  104.           (
  105.             t.montant_ht_actualise_echeance *(
  106.               SELECT
  107.                 CAST(
  108.                   REPLACE(icc_multiplicateur, ',', '.') AS DECIMAL(10, 4)
  109.                 )
  110.               FROM
  111.                 indice_icc
  112.               WHERE
  113.                 REPLACE(
  114.                   t.periode_courante_indice_echeance,
  115.                   SUBSTRING(t.periode_courante_indice_echeance, 1, 4),(
  116.                     CAST(
  117.                       SUBSTRING(t.periode_courante_indice_echeance, 1, 4) AS SIGNED INTEGER
  118.                     ) + 1
  119.                   )
  120.                 ) = icc_periode
  121.             )
  122.           ),
  123.           2
  124.         ) ELSE ROUND((t.montant_ht_actualise_echeance *(1.02)), 2) END ELSE ROUND((t.montant_ht_actualise_echeance *(1.02)), 2) END
  125.       ) montant_ht_actualise_echeance,
  126.       t.date_quittancement_echeance2,
  127.       t.libelle_rubrique_echeance,
  128.       t.code_indice,
  129.       t.nom_tiers
  130.     FROM
  131.       (
  132.         SELECT
  133.           g.num_version_contrat,
  134.           g.num_contrat,
  135.           g.libelle_contrat,
  136.           g.libelle_dr,
  137.           g.code_dr,
  138.           g.libelle_type_contrat,
  139.           g.date_effet_contrat,
  140.           g.date_fin_contrat,
  141.           g.libelle_etat_contrat,
  142.           g.flag_pris_en_compte_dans_V3_V2,
  143.           g.date_debut_periode_echeance,
  144.           g.date_fin_periode_echeance,
  145.           g.date_quittancement_echeance,
  146.           MAX(g.date_quittancement_echeance) date_quittancement_echeance2,
  147.           (
  148.             CASE WHEN MAX(g.date_quittancement_echeance) > DATE(NOW()) THEN 0 WHEN g.num_version_contrat IN (
  149.               SELECT
  150.                 DISTINCT gid3.num_version_contrat
  151.               FROM
  152.                 base_gid gid3
  153.               WHERE
  154.                 EXISTS (
  155.                   SELECT
  156.                     *
  157.                   FROM
  158.                     base_gid gid2
  159.                   WHERE
  160.                     gid2.libelle_rubrique_echeance <> gid3.libelle_rubrique_echeance
  161.                     AND gid2.num_version_contrat = gid3.num_version_contrat
  162.                     AND gid2.date_quittancement_echeance <> gid3.date_quittancement_echeance
  163.                     AND gid2.code_nidt = gid3.code_nidt
  164.                     AND gid2.nom_tiers = gid3.nom_tiers
  165.                     AND gid2.libelle_rubrique_echeance IN ('LOYER NET', 'LOYER HT')
  166.                 )
  167.             ) THEN 0 ELSE 1 END
  168.           ) libelle_rubrique_echeance2,
  169.           g.montant_ht_actualise_echeance,
  170.           g.code_indice,
  171.           g.code_nidt,
  172.           b.nom_site,
  173.           g.nom_tiers,
  174.           g.libelle_rubrique_echeance,
  175.           g.periode_courante_indice_echeance,
  176.           groupe1.dernier_montant
  177.         FROM
  178.           base_bde b
  179.           LEFT JOIN base_gid g ON b.code_nidt = g.code_nidt
  180.           INNER JOIN (
  181.             SELECT
  182.               num_version_contrat,
  183.               MAX(date_quittancement_echeance) AS maxDate,
  184.               montant_ht_actualise_echeance AS dernier_montant
  185.             FROM
  186.               base_gid g4
  187.             GROUP BY
  188.               num_version_contrat
  189.           ) groupe1 ON (
  190.             g.num_version_contrat = groupe1.num_version_contrat
  191.           )
  192.           AND (g.date_quittancement_echeance = groupe1.maxDate)
  193.         WHERE
  194.           (
  195.             g.libelle_etat_contrat IN ('Actif', 'En cours de création')
  196.           )
  197.           AND (g.flag_pris_en_compte_dans_V3_V2 = 1)
  198.           AND (g.libelle_contrat NOT LIKE "%ORANGE REUNION%")
  199.           AND (g.libelle_contrat NOT LIKE "%ORANGE CARAIBES%")
  200.           AND (
  201.             g.libelle_rubrique_echeance IN (
  202.               'LOYER HT',
  203.               'CHARGES HT',
  204.               'ENERGIE HT',
  205.               'CHARGES DOM HT',
  206.               'CHARGES NETTES',
  207.               'ENERGIE NETTE',
  208.               'LOYER NET',
  209.               'LOYER SOL HT',
  210.               'LOYER HT (TVA 19%)'
  211.             )
  212.           )
  213.           AND (
  214.             g.code_nidt NOT IN (
  215.               SELECT
  216.                 code_nidt
  217.               FROM
  218.                 sites_logiques
  219.             )
  220.           )
  221.           AND (g.date_fin_contrat > DATE('2019-01-01'))
  222.           AND (g.timestampdiff_debut_fin_periode_echeance < 24)
  223.         GROUP BY
  224.           g.num_version_contrat,
  225.           g.libelle_rubrique_echeance
  226.         HAVING
  227.           TIMESTAMPDIFF(
  228.             MONTH,
  229.             MAX(g.date_quittancement_echeance),
  230.             g.date_fin_contrat
  231.           ) > 13
  232.       ) AS t
  233.     WHERE
  234.       (
  235.         date_quittancement_echeance2 < DATE('2019-12-31')
  236.       )
  237.       AND (libelle_rubrique_echeance2 = 1)
  238.     GROUP BY
  239.       t.num_version_contrat,
  240.       t.date_debut_periode_echeance,
  241.       t.date_fin_periode_echeance,
  242.       t.montant_ht_actualise_echeance,
  243.       t.libelle_rubrique_echeance,
  244.       t.nom_tiers
  245.   ) AS t2
  246. GROUP BY
  247.   t2.code_dr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement