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