fork download
  1.  
  2. WITH rescheduled AS (
  3. SELECT
  4. cap.customer_id AS customer_id,
  5. cap.created_at::date AS date_of_reschedule,
  6. old_loan.id AS old_loan_id,
  7. new_loan.id AS new_loan_id
  8. FROM loan_app_loan AS new_loan
  9. INNER JOIN crm_app_purchase cap
  10. ON new_loan.id = cap.loan_id
  11. INNER JOIN crm_app_purchase cap2
  12. ON cap2.id = cap.parent_purchase_id
  13. INNER JOIN loan_app_loan AS old_loan
  14. ON old_loan.id = cap2.loan_id
  15. WHERE
  16. cap2.is_rescheduled = TRUE
  17. AND cap.created_at::date BETWEEN {'date':'start_date'} AND {'date':'end_date'}
  18. ),
  19.  
  20.  
  21. old_enr AS (
  22. SELECT
  23. i.loan_id,
  24. sum(i.principal_expected + i.interest_expected) AS old_loan_enr
  25. FROM loan_app_installment i
  26. JOIN rescheduled r
  27. ON r.old_loan_id = i.loan_id
  28. join new_lms_installmentextension ie on ie.installment_ptr_id = i.id
  29. WHERE i.day <= r.date_of_reschedule and ie.status_id = 16
  30. GROUP BY i.loan_id
  31. ),
  32.  
  33.  
  34.  
  35. old_interest AS (
  36. SELECT
  37. i.loan_id,
  38. SUM(i.interest_expected)::numeric AS old_loan_interest
  39. FROM new_lms_installmentextension ie
  40. join loan_app_installment i on i.id = ie.installment_ptr_id
  41. JOIN rescheduled r
  42. ON r.old_loan_id = i.loan_id
  43. WHERE
  44. i.day <= r.date_of_reschedule
  45. AND ie.status_id = 16
  46. GROUP BY i.loan_id
  47. ),
  48.  
  49.  
  50. old_late_fees AS (
  51. SELECT
  52. i.loan_id,
  53. SUM(lf.amount)::numeric AS old_loan_late_fees
  54. FROM new_lms_installmentlatefees lf
  55. JOIN new_lms_installmentextension ie
  56. ON lf.installment_extension_id = ie.installment_ptr_id
  57.  
  58. join loan_app_installment i on i.id = lf.installment_extension_id
  59. JOIN rescheduled r
  60. ON r.old_loan_id = i.loan_id
  61. where
  62. ie.status_id = 16
  63. -- lf.is_paid = FALSE
  64. -- AND lf.is_cancelled = FALSE
  65. -- AND lf.is_disabled = FALSE
  66. AND lf.day <= r.date_of_reschedule
  67. GROUP BY i.loan_id
  68. )
  69.  
  70. SELECT
  71. r.customer_id AS "Customer ID",
  72. r.date_of_reschedule AS "Date of Reschedule",
  73.  
  74.  
  75. old_loan.id AS "Old Loan ID",
  76. COALESCE(oe.old_loan_enr, old_loan.principle) AS "Old Loan ENR",
  77. COALESCE(oi.old_loan_interest, 0) AS "Old Loan Interest",
  78. COALESCE(ol.old_loan_late_fees, 0) AS "Old Loan Late Fees",
  79. old_loan.principle AS "Old Loan Principal",
  80. old_loan.period_installment AS "Old Installment",
  81. old_loan.num_months AS "Old Loan Tenor",
  82. old_loan.period_interest_rate AS "Old Loan Period Interest Rate",
  83. old_loan.yearly_interest AS "Old Loan Yearly Interest",
  84. old_status.name AS "Old Loan Status",
  85.  
  86.  
  87. new_loan.id AS "New Loan ID",
  88. new_loan.principle AS "New Loan Principal",
  89. new_loan.period_installment AS "New Installment",
  90. new_loan.num_months AS "New Loan Tenor",
  91. new_loan.period_interest_rate AS "New Loan Period Interest Rate",
  92. new_loan.yearly_interest AS "New Loan Yearly Interest",
  93. new_status.name AS "New Loan Status"
  94.  
  95. FROM rescheduled r
  96. JOIN loan_app_loan old_loan
  97. ON old_loan.id = r.old_loan_id
  98. JOIN loan_app_loan new_loan
  99. ON new_loan.id = r.new_loan_id
  100. JOIN loan_app_loanstatus old_status
  101. ON old_status.id = old_loan.status_id
  102. JOIN loan_app_loanstatus new_status
  103. ON new_status.id = new_loan.status_id
  104. LEFT JOIN old_enr oe
  105. ON oe.loan_id = r.old_loan_id
  106. LEFT JOIN old_interest oi
  107. ON oi.loan_id = r.old_loan_id
  108. LEFT JOIN old_late_fees ol
  109. ON ol.loan_id = r.old_loan_id
  110. ORDER BY
  111. r.date_of_reschedule,
  112. r.customer_id,
  113. r.old_loan_id;
Success #stdin #stdout #stderr 0s 5332KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 2: unrecognized token: ":"