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. MIN(i.remaining_principal_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. WHERE i.day >= r.date_of_reschedule
  29. GROUP BY i.loan_id
  30. ),
  31.  
  32.  
  33. old_interest AS (
  34. SELECT
  35. i.loan_id,
  36. SUM(i.interest_expected)::numeric AS old_loan_interest
  37. FROM new_lms_installmentextension ie
  38. join loan_app_installment i on i.id = ie.installment_ptr_id
  39. JOIN rescheduled r
  40. ON r.old_loan_id = i.loan_id
  41. WHERE
  42. i.day <= r.date_of_reschedule
  43. AND ie.status_id = 16
  44. GROUP BY i.loan_id
  45. ),
  46.  
  47.  
  48. old_late_fees AS (
  49. SELECT
  50. i.loan_id,
  51. SUM(lf.amount)::numeric AS old_loan_late_fees
  52. FROM new_lms_installmentlatefees lf
  53. JOIN new_lms_installmentextension ie
  54. ON lf.installment_extension_id = ie.installment_ptr_id
  55.  
  56. join loan_app_installment i on i.id = lf.installment_extension_id
  57. JOIN rescheduled r
  58. ON r.old_loan_id = i.loan_id
  59. where
  60. ie.status_id = 16
  61. -- lf.is_paid = FALSE
  62. -- AND lf.is_cancelled = FALSE
  63. -- AND lf.is_disabled = FALSE
  64. AND lf.day <= r.date_of_reschedule
  65. GROUP BY i.loan_id
  66. )
  67.  
  68. SELECT
  69. r.customer_id AS "Customer ID",
  70. r.date_of_reschedule AS "Date of Reschedule",
  71.  
  72.  
  73. old_loan.id AS "Old Loan ID",
  74. COALESCE(oe.old_loan_enr, old_loan.principle) AS "Old Loan ENR",
  75. COALESCE(oi.old_loan_interest, 0) AS "Old Loan Interest",
  76. COALESCE(ol.old_loan_late_fees, 0) AS "Old Loan Late Fees",
  77. old_loan.principle AS "Old Loan Principal",
  78. old_loan.period_installment AS "Old Installment",
  79. old_loan.num_months AS "Old Loan Tenor",
  80. old_loan.period_interest_rate AS "Old Loan Period Interest Rate",
  81. old_loan.yearly_interest AS "Old Loan Yearly Interest",
  82. old_status.name AS "Old Loan Status",
  83.  
  84.  
  85. new_loan.id AS "New Loan ID",
  86. new_loan.principle AS "New Loan Principal",
  87. new_loan.period_installment AS "New Installment",
  88. new_loan.num_months AS "New Loan Tenor",
  89. new_loan.period_interest_rate AS "New Loan Period Interest Rate",
  90. new_loan.yearly_interest AS "New Loan Yearly Interest",
  91. new_status.name AS "New Loan Status"
  92.  
  93. FROM rescheduled r
  94. JOIN loan_app_loan old_loan
  95. ON old_loan.id = r.old_loan_id
  96. JOIN loan_app_loan new_loan
  97. ON new_loan.id = r.new_loan_id
  98. JOIN loan_app_loanstatus old_status
  99. ON old_status.id = old_loan.status_id
  100. JOIN loan_app_loanstatus new_status
  101. ON new_status.id = new_loan.status_id
  102. LEFT JOIN old_enr oe
  103. ON oe.loan_id = r.old_loan_id
  104. LEFT JOIN old_interest oi
  105. ON oi.loan_id = r.old_loan_id
  106. LEFT JOIN old_late_fees ol
  107. ON ol.loan_id = r.old_loan_id
  108. ORDER BY
  109. r.date_of_reschedule,
  110. r.customer_id,
  111. r.old_loan_id;
Success #stdin #stdout #stderr 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 2: unrecognized token: ":"