fork download
  1. --select * from tms_app_loaninstallmentfundingrequest --
  2.  
  3. with loan_on_day_status as (
  4. select id as loan_id,
  5. (
  6. select status_id
  7. from loan_app_loanstatushistroy
  8. where day <= {'date':'date'} and status_type = 0 and loan_app_loanstatushistroy.loan_id = loan_app_loan.id
  9. order by loan_id, updated_at desc
  10. limit 1
  11. ) as blnk_status,
  12. (
  13. select day
  14. from loan_app_loanstatushistroy
  15. where day <= {'date':'date'} and status_type = 0 and loan_app_loanstatushistroy.loan_id = loan_app_loan.id
  16. order by loan_id, updated_at desc
  17. limit 1
  18. ) as blnk_status_day,
  19. (
  20. select status_id
  21. from loan_app_loanstatushistroy
  22. where day <= {'date':'date'} and status_type = 1 and loan_app_loanstatushistroy.loan_id = loan_app_loan.id
  23. order by loan_id, updated_at desc
  24. limit 1
  25. ) as fra_status
  26. from loan_app_loan
  27. order by loan_id
  28. ),
  29.  
  30. latest_payment_status as (
  31. select distinct on (nli.installment_extension_id) installment_extension_id as installment_id, status as payment_status, nli.day
  32. from new_lms_installmentpaymentstatushistory nli
  33. where day <= {'date':'date'}
  34. order by nli.installment_extension_id, nli.day desc, nli.id desc
  35. )
  36. ,
  37.  
  38.  
  39. -- without bv
  40. off_bal_installments as (
  41. select lai.id
  42. from loan_app_installment lai
  43. inner join tms_app_loanmerchantbatch talmb on talmb.loan_id = lai.loan_id and talmb.bond_id = 16
  44. union
  45. select talifr.installment_id as id
  46. from tms_app_loaninstallmentfundingrequest talifr
  47. where talifr.onbalance = false and assignment_day <= {'date':'date'}
  48.  
  49. ),
  50.  
  51. on_bal_installments as (
  52. select id
  53. from loan_app_installment
  54. except
  55. select id
  56. from off_bal_installments
  57. )
  58. ,
  59. targeted_loans as (
  60. select distinct lai.loan_id as loan_id
  61. from loan_app_installment lai
  62. inner join on_bal_installments obi on obi.id = lai.id
  63.  
  64. )
  65. ,
  66. remaining_principal as (
  67. select lal.id, coalesce(sum(lai.principal_expected) filter (where nlie.principal_paid_at is null or cast(principal_paid_at as date) > {'date':'date'}
  68. or
  69. ( cast(principal_paid_at as date) < lai.day and (nlie.payment_status not in (2,3,4,16) and lds.blnk_status not in (1,6,8,15,16) or
  70. (nlie.payment_status = 6 ) ))
  71. and {'date':'date'} < lai.day
  72. ), 0) as enr,
  73.  
  74. coalesce( sum(lai.principal_expected) filter ( where lps.payment_status in (5,6) and nlie.is_long_term=false and (nlie.status_id <> 7 or (nlie.status_id = 7 and (is_principal_paid=false or (principal_paid_at::date > {'date':'date'})))) and ((( nlie.status_id=7 or nlie.long_to_short_term_date < {'date':'date'}) and nlie.long_to_short_term_date < {'date':'date'} ) or nlie.long_to_short_term_date is null)
  75. ),0) as undue_principle,
  76.  
  77.  
  78. coalesce( sum(lai.principal_expected) filter (
  79. where
  80. lps.payment_status = 0 and {'date':'date'} >= nlie.due_to_overdue_date
  81. and (((nlie.status_id=7 or nlie.long_to_short_term_date < {'date':'date'}) and nlie.long_to_short_term_date < {'date':'date'} ) or nlie.long_to_short_term_date is null) and (is_principal_paid=false or (principal_paid_at::date > {'date':'date'}))
  82. ) ,0) as overdue_principle,
  83.  
  84. coalesce( sum(lai.principal_expected) filter (
  85. where
  86. (lps.payment_status = 4 and (nlie.is_principal_paid = false or nlie.principal_paid_at > {'date':'date'}) ) or (lps.payment_status = 0 and {'date':'date'} < nlie.due_to_overdue_date )
  87. -- )
  88. ) ,0) as due_principle,
  89.  
  90. coalesce(
  91. sum(lai.principal_expected) filter (
  92. where
  93. (nlie.is_long_term = true or (nlie.is_long_term = false and nlie.long_to_short_term_date >= {'date':'date'}))
  94. and (nlie.is_principal_paid = false or cast(nlie.principal_paid_at as date) > {'date':'date'} )
  95. ),
  96. 0
  97. ) as long_term_principle
  98.  
  99.  
  100.  
  101. from loan_app_loan lal
  102. inner join loan_on_day_status lds on lds.loan_id = lal.id
  103. inner join loan_app_installment lai on lai.loan_id = lal.id
  104. inner join latest_payment_status lps on lps.installment_id = lai.id
  105. inner join new_lms_installmentextension nlie on nlie.installment_ptr_id = lai.id
  106. inner join on_bal_installments obi on obi.id = lai.id
  107. where lds.blnk_status not in (12,13)
  108.  
  109. group by lal.id
  110.  
  111. )
  112. ,
  113. provisions as (
  114.  
  115. select lal.id, coalesce(sum(lai.principal_expected) filter (where nlie.principal_paid_at is null or cast(principal_paid_at as date) > {'date':'date'}
  116. or
  117. ( cast(principal_paid_at as date) < lai.day and (nlie.payment_status not in (2,3,4,16) and lds.blnk_status not in (1,6,8,15,16) or
  118. (nlie.payment_status = 6 and lds.blnk_status not in (1,6,8,15,16) ) ))
  119. and {'date':'date'} < lai.day
  120. ), 0) as enr
  121.  
  122. from loan_app_loan lal
  123. inner join loan_app_installment lai on lai.loan_id = lal.id
  124. inner join new_lms_installmentextension nlie on nlie.installment_ptr_id = lai.id
  125. inner join loan_on_day_status lds on lds.loan_id = lal.id
  126. group by lal.id
  127.  
  128. )
  129. ,
  130.  
  131.  
  132. late_fees as (
  133. select lal.id, sum(nlilf.amount) filter(
  134. where nlilf.day < {'date':'date'} and lds.blnk_status not in (7,17)
  135. and (nlilf.is_cancelled = false or cast(nlilf.cancellation_date as date) > {'date':'date'}) and nlilf.is_disabled = false
  136. and (nlilf.is_partially_cancelled = false or nlilf.partially_cancelled_date > {'date':'date'})
  137. and (nlilf.is_paid = false or cast(nlilf.paid_at as date) > {'date':'date'})
  138. )as total_late_fees,
  139.  
  140. sum(nlilf.amount) filter
  141. (where lds.blnk_status <> 7 and ((nlilf.day<{'date':'date'} and {'date':'date'}<'2025-09-30' and (select count(*) from loan_app_loanstatushistroy where loan_id=lal.id and
  142. status_type=0 and day <={'date':'date'} and status_id =3 and day < '2025-09-30') >0 and (nlilf.is_paid=false or nlilf.paid_at::date>{'date':'date'})) or ((nlilf.is_marginalized = true
  143. and cast(nlilf.marginalization_date as date) <= {'date':'date'}) or (
  144. nlilf.is_marginalized = false
  145. and cast(nlilf.marginalization_date as date) is not null
  146. and cast(nlilf.marginalization_date as date) <= {'date':'date'}
  147. and cast(nlilf.unmarginalization_date as date) > {'date':'date'}) ))and (nlilf.is_cancelled =false or (nlilf.is_cancelled =true and nlilf.cancellation_date> {'date':'date'})) )
  148. as marginalized
  149. from new_lms_installmentlatefees nlilf
  150. inner join loan_app_installment lai on lai.id = nlilf .installment_extension_id
  151. inner join loan_app_loan lal on lal.id = lai.loan_id
  152. inner join on_bal_installments obi on obi.id = lai.id
  153. inner join loan_on_day_status lds on lds.loan_id = lal.id
  154.  
  155. group by lal.id
  156. )
  157. ,
  158.  
  159. accrued_interest as(
  160. select lai.loan_id as id, sum((
  161. case when nlie.payment_status in (0,2) and nlie.partial_accrual_date <= {'date':'date'} then (
  162. (case when nlie.accrual_date <= {'date':'date'} then nlie.actual_accrued_amount else 0 end) + nlie.partial_accrual_amount - (case when cast(nlie.interest_paid_at as date) <= {'date':'date'} then nlie.actual_interest_paid else 0 end) + nlie.settlement_accrual_interest_amount + nlie.faulty_accrued_amount
  163. )
  164. when nlie.payment_status in (4) and nlie.partial_accrual_date <= {'date':'date'} then
  165. (nlie.partial_accrual_amount + (case when nlie.accrual_date <= {'date':'date'} then nlie.actual_accrued_amount else 0 end)
  166. )
  167. when nlie.payment_status in (1)then
  168. (
  169. (case when nlie.partial_accrual_date <= {'date':'date'} then nlie.partial_accrual_amount else 0 end)
  170. + (case when nlie.accrual_date <= {'date':'date'} then nlie.actual_accrued_amount - nlie.actual_interest_paid + nlie.faulty_accrued_amount else 0 end)
  171. )
  172. when nlie.payment_status in (3,16) then
  173. case when nlie.partial_accrual_date <= {'date':'date'} then nlie.partial_accrual_amount else 0 end
  174. + (case when nlie.accrual_date <= {'date':'date'} then nlie.actual_accrued_amount else 0 end)
  175. + case when cast(nlie.settlement_accrual_interest_date as date) <= {'date':'date'} then nlie.settlement_accrual_interest_amount else 0 end
  176. - (case when cast(nlie.interest_paid_at as date) <= {'date':'date'} then nlie.actual_interest_paid else 0 end)
  177.  
  178. end
  179. ))
  180. as accrued_interest
  181. from loan_app_installment lai
  182. inner join new_lms_installmentextension nlie on nlie.installment_ptr_id = lai.id
  183. inner join loan_app_loan lal on lal.id = lai.loan_id
  184. group by lai.loan_id
  185. )
  186. ,
  187.  
  188. decomposed_interests as(
  189. select lai.loan_id ,
  190. sum(case when nlie.due_to_overdue_date <= {'date':'date'} and (lds.blnk_status <> 7 or ( lds.blnk_status = 7 and nlie.due_to_overdue_date < lds.blnk_status_day))
  191. and (nlie.is_interest_paid = false or cast(nlie.interest_paid_at as date) > {'date':'date'}) and lds.blnk_status not in (6,8,12,13,15,16)
  192. then lai.interest_expected + round(case when extra_interest_paid_at<= {'date':'date'} then 0 else nlie.first_installment_interest_adjustment end,2) end) as overdue_interest,
  193.  
  194.  
  195. sum(case when {'date':'date'} >= nlie.undue_to_due_date and {'date':'date'} <= nlie.grace_date and (nlie.is_interest_paid = false or cast(nlie.interest_paid_at as date) > {'date':'date'}) and lds.blnk_status not in (6,8,12,13,15,16) then lai.interest_expected + round(nlie.first_installment_interest_adjustment,2) else 0 end) as due_nonpaid_interest
  196. from loan_app_installment lai
  197. inner join new_lms_installmentextension nlie on nlie.installment_ptr_id = lai.id
  198. inner join loan_app_loan lal on lal.id = lai.loan_id
  199. inner join loan_on_day_status lds on lds.loan_id = lal.id
  200. inner join on_bal_installments obi on obi.id = lai.id
  201.  
  202. where lds.blnk_status not in (7,12,13)
  203.  
  204. group by lai.loan_id
  205. -- select lai.loan_id ,
  206. -- sum(case when nlie.due_to_overdue_date <= {'date':'date'}
  207. -- and (nlie.is_interest_paid = false or nlie.interest_paid_at::date > {'date':'date'}) and lds.blnk_status not in (6,8,12,13,15)
  208. -- then lai.interest_expected end) as overdue_interest,
  209. -- sum(
  210. -- case
  211. -- when
  212. -- {'date':'date'} >= nlie.undue_to_due_date and {'date':'date'} < nlie.due_to_overdue_date
  213. -- and (nlie.is_interest_paid = false or nlie.interest_paid_at::date > {'date':'date'})
  214. -- and lds.blnk_status not in (6,7,8,12,13,15)
  215. -- then lai.interest_expected
  216. -- else 0
  217. -- end
  218. -- ) as due_nonpaid_interest
  219. -- from loan_app_installment lai
  220. -- inner join new_lms_installmentextension nlie on nlie.installment_ptr_id = lai.id
  221. -- inner join loan_app_loan lal on lal.id = lai.loan_id
  222. -- inner join on_bal_installments obi on obi.id = lai.id
  223.  
  224. -- inner join loan_on_day_status lds on lds.loan_id = lal.id
  225. -- where lds.blnk_status not in (12,13)
  226.  
  227. -- group by lai.loan_id
  228. )
  229. ,
  230.  
  231. undue_interest as (
  232. with installments_details as (
  233. select
  234. lai.id as "installment_id",
  235. lai.loan_id,
  236. lal.status_id,
  237. nli.accrual_date,
  238. lal.first_accrual_adjustment_date,
  239. nli.is_extra_interest_paid,
  240. nli.payment_status,
  241. nli.first_installment_interest_adjustment,
  242. nli.actual_interest_paid,
  243. nli.settlement_accrual_interest_amount,
  244. nli.settlement_accrual_interest_date,
  245. nli.expected_partial_accrual_amount,
  246. lai.interest_expected,
  247. lai.day,
  248. nli.interest_paid_at,
  249. nli.partial_accrual_date,
  250. lai."period",
  251. nli.expected_accrual_amount,
  252. nli.extra_interest_paid_at,
  253. nli.undue_to_due_date
  254. from new_lms_installmentextension nli
  255. inner join loan_app_installment lai on nli.installment_ptr_id = lai.id
  256. inner join loan_app_loan lal on lal.id = lai.loan_id
  257. inner join on_bal_installments obi on obi.id = lai.id
  258.  
  259. )
  260. ,
  261. init_accrual as(
  262. select installments_details.loan_id lalid1, sum(first_installment_interest_adjustment) first_accrual_adjustment
  263. from installments_details
  264. inner join latest_payment_status lps on lps.installment_id = installments_details."installment_id"
  265. inner join loan_on_day_status lods on installments_details.loan_id = lods.loan_id
  266. where
  267. (lps.payment_status in (5,6) or(lps.payment_status in (4,7) and lods.blnk_status in (11,14,7,15) and accrual_date > {'date':'date'}) or (lps.payment_status=10 and extra_interest_paid_at::date > {'date':'date'}) or (lps.payment_status in (3,16) and accrual_date > {'date':'date'} and extra_interest_paid_at::date > {'date':'date'})) and first_installment_interest_adjustment > 0
  268. and (lods.blnk_status not in (8,15,16) or (lods.blnk_status in (8,15,16) and is_extra_interest_paid = true and extra_interest_paid_at::date > {'date':'date'})) and first_accrual_adjustment_date <= {'date':'date'}
  269. and (partial_accrual_date > {'date':'date'} or (partial_accrual_date = {'date':'date'} and EXTRACT('DAY' FROM undue_to_due_date) = 1)) and lps.payment_status not in (12,13)
  270. group by installments_details.loan_id
  271. )
  272. ,
  273. next_inst as(
  274. select lalid3, insts from (
  275. select nli.loan_id lalid3, (expected_accrual_amount) insts, lods.blnk_status status_on_day,
  276. (select status from new_lms_installmentpaymentstatushistory where installment_extension_id=nli.installment_id and day <={'date':'date'} order by id desc limit 1) as last_status,
  277. undue_to_due_date, interest_paid_at
  278. from installments_details nli
  279. inner join loan_on_day_status lods on nli.loan_id = lods.loan_id
  280. where accrual_date = {'date':'date'}
  281. and (lods.blnk_status not in (15,7)
  282. or (lods.blnk_status = 15 and (settlement_accrual_interest_amount = 0 or settlement_accrual_interest_amount <> 0 and settlement_accrual_interest_date::date > {'date':'date'}))
  283. or (lods.blnk_status = 7 and lods.blnk_status_day > accrual_date )
  284. ) ) s
  285. where last_status not in (1,2,3,7,12)
  286. or
  287. (last_status = 2
  288. and (interest_paid_at::date < undue_to_due_date or (interest_paid_at is null and status_on_day <>15))
  289. )
  290. or (last_status = 7 and (select status_id from loan_app_loan where id = lalid3) <>15)
  291. )
  292. ,
  293. next_partial_only as (
  294. select lalid4, insts from (
  295. select nli.loan_id lalid4, expected_partial_accrual_amount + (case when nli.is_extra_interest_paid=true and nli.extra_interest_paid_at::date <={'date':'date'} then 0 else
  296. round(first_installment_interest_adjustment,2) end) insts,
  297. (select status from new_lms_installmentpaymentstatushistory where installment_extension_id=nli.installment_id and day <={'date':'date'} order by id desc limit 1) as last_status,
  298. undue_to_due_date, interest_paid_at
  299. from installments_details nli
  300. inner join loan_on_day_status lods on nli.loan_id = lods.loan_id
  301. where
  302. (select nlish.status_id from new_lms_installmentstatushistory nlish where nlish.installment_id = nli.installment_id and day <= {'date':'date'} and status_type = 1 order by id desc limit 1) <> 7
  303. and partial_accrual_date <= {'date':'date'}
  304. and day > cast({'date':'date'} as date) + interval '1 day'
  305. and (lods.blnk_status <> 15 or (lods.blnk_status = 15
  306. and (settlement_accrual_interest_amount = 0 or settlement_accrual_interest_amount <> 0 and cast(settlement_accrual_interest_date as date) > {'date':'date'}))
  307. )
  308. )
  309. s where last_status not in (1,2,3,7,12)
  310. or (last_status = 2
  311. and (cast(interest_paid_at as date) < undue_to_due_date or (interest_paid_at is null and (select blnk_status from loan_on_day_status where loan_id = lalid4) <>15)))
  312. or (last_status = 7 and (select blnk_status from loan_on_day_status where loan_id = lalid4) <>15)
  313. )
  314. ,
  315. old_first_accrual_in_partial as(
  316. select lalid5, insts from (
  317. select loan_id lalid5, (expected_partial_accrual_amount) insts,
  318. (select status from new_lms_installmentpaymentstatushistory where installment_extension_id=nli.installment_id and day <={'date':'date'} order by id desc limit 1) as last_status
  319. from installments_details nli
  320. where accrual_date = {'date':'date'} and expected_partial_accrual_amount <> 0 and (status_id not in (8,16) or (status_id in (8,16) and payment_status not in (3,16)))
  321. ) s where last_status not in (12,13)
  322. )
  323.  
  324.  
  325. SELECT loan, sum(undue_balance) as undue_balance
  326. FROM(
  327. select lalid1 loan, first_accrual_adjustment undue_balance from init_accrual
  328. UNION ALL
  329. select lalid3 loan, insts undue_balance from next_inst
  330. UNION ALL
  331. select lalid4 loan, insts undue_balance from next_partial_only
  332. UNION ALL
  333. select lalid5 loan, insts undue_balance from old_first_accrual_in_partial
  334. ) s group by s.loan
  335.  
  336. )
  337.  
  338.  
  339. ,
  340. marginalized_interest as(
  341. select lai.loan_id ,
  342. sum(
  343. case
  344. -- the first 2 lines here are to get all the marginalization that was done before applying 90 days marginalization on 30-09-2025
  345. when lds.blnk_status > 2 and lai.day <={'date':'date'} and {'date':'date'} < '2025-09-30' and (nlie.is_interest_paid=false or nlie.interest_paid_at::date > {'date':'date'}) then lai.interest_expected + round(nlie.first_installment_interest_adjustment,2)
  346. when nlie.partial_accrual_date<={'date':'date'} and lai.day > {'date':'date'} and lds.blnk_status > 2 and {'date':'date'} < '2025-09-30' and (nlie.is_interest_paid=false or nlie.interest_paid_at::date > {'date':'date'}) then expected_partial_accrual_amount
  347. when is_marginalized=true and marginalization_date <= {'date':'date'} then lai.interest_expected + round(nlie.first_installment_interest_adjustment,2)
  348. when is_partially_marginalized=true and partial_marginalization_date <= {'date':'date'} then expected_partial_accrual_amount
  349. when is_marginalized=false and marginalization_date <= {'date':'date'} and unmarginalization_date > {'date':'date'} then lai.interest_expected + round(nlie.first_installment_interest_adjustment,2)
  350. when is_partially_marginalized=false and partial_marginalization_date <= {'date':'date'} and unmarginalization_date > {'date':'date'} then expected_partial_accrual_amount
  351. else 0
  352. end
  353. )as marginalized_interest
  354. -- sum(
  355. -- (
  356. -- case when nlie.partial_marginalization_date is not null and (nlie.is_partially_marginalized = true or nlie.unmarginalization_date::date > {'date':'date'}) and nlie.partial_marginalization_date::date < {'date':'date'}
  357. -- then nlie.partial_accrual_amount else 0 end
  358. -- )
  359. -- +(
  360. -- case when nlie.marginalization_date is not null and (nlie.is_marginalized = true or nlie.unmarginalization_date::date > {'date':'date'}) and nlie.marginalization_date::date < {'date':'date'}
  361. -- then nlie.actual_accrued_amount + (
  362. -- case when (nlie.partial_marginalization_date::date is null or nlie.unmarginalization_date::date < nlie.marginalization_date) and nlie.is_partially_marginalized = False
  363. -- then nlie.partial_accrual_amount else 0 end
  364. -- ) else 0 end
  365. -- )
  366. -- ) as marginalized_interest
  367. from loan_app_installment lai
  368. inner join new_lms_installmentextension nlie on nlie.installment_ptr_id = lai.id
  369. inner join on_bal_installments obi on obi.id = lai.id
  370. inner join loan_on_day_status lds on lds.loan_id = lai.loan_id
  371. where lds.blnk_status <> 7
  372.  
  373. group by lai.loan_id
  374. )
  375. ,
  376.  
  377.  
  378.  
  379. main_query as (
  380. select lal.id
  381. ,
  382. -- (case when lds.blnk_status=7 then coalesce(rp.enr,0) else
  383. (coalesce((coalesce(rp.undue_principle,0)+coalesce(rp.overdue_principle,0)+coalesce(rp.due_principle,0)+coalesce(rp.long_term_principle,0)),0))as ENR,
  384. coalesce(rp.undue_principle,0) as "Loans receivable undue, (gross)",
  385. coalesce(rp.overdue_principle,0) as "Loans receivable overdue, (gross)",
  386. coalesce(rp.due_principle,0) as "Loans receivable due, (gross)",
  387. coalesce(rp.long_term_principle,0) as "Long term loans receivables",
  388. coalesce(di.due_nonpaid_interest,0) as "Accrued interest income receivable, due (gross)",
  389. coalesce(ui.undue_balance,0) as "Accrued interest income receivable, undue (gross)",
  390. coalesce(mi.marginalized_interest,0) as "Marginalized interest income receivable",
  391. coalesce(lf.total_late_fees,0) as total_late_fees,
  392. coalesce(lf.marginalized,0) as "Marginalized late repayment fee income receivable",
  393. round(lap.percentage * coalesce((coalesce(rp.undue_principle,0)+coalesce(rp.overdue_principle,0)+coalesce(rp.due_principle,0)+coalesce(rp.long_term_principle,0)),0) / 100,2) as "Impairment provisions, On-balance sheet",
  394. coalesce(di.overdue_interest,0) as "Accrued interest income receivable, overdue (gross)",
  395. lalsb.name as blnk_status,
  396. lalsf.name as fra_status,
  397. lal.principle as loan_amount,
  398. lal.yearly_interest,
  399. lal.loan_booking_day,
  400. lal.period_installment,
  401. lal.num_months,
  402. --case when talmb.bond_id = 15 then 'securitized' when talmb.bond_id = 16 then 'BV' else 'On Balance' end as portfolio,
  403. cac.id as customer,
  404. case when cac.limit_source = 1 then 'Banked' when cac.limit_source = 2 then 'Unbanked' end as limit_source,
  405. concat(cac.first_name,' ',cac.last_name) as customer_name,
  406. cac.phone_number,
  407. nllc.starting_day_of_the_month as cycle_day,
  408. cacr.name as rank
  409. , cam3.name_ar as product_type
  410. from loan_app_loan lal
  411. inner join targeted_loans tl on tl.loan_id = lal.id
  412.  
  413. inner join loan_on_day_status lds on lds.loan_id = lal.id
  414. left join loan_app_loanstatus lalsb on lalsb.id = lds.blnk_status
  415. left join loan_app_loanstatus lalsf on lalsf.id = lds.fra_status
  416. inner join crm_app_customer cac on cac.id = lal.customer_id
  417. inner join new_lms_loancycle nllc on nllc.id = cac.first_loan_cycle_id
  418. inner join crm_app_customerrank cacr on cacr.id = cac.rank_id
  419. inner join loan_app_provision lap on lap.status_id = lds.fra_status
  420. left join crm_app_merchant cam2 on cam2.id = lal.merchant_id
  421. left join crm_app_merchantcategory cam3 on cam3.id = cam2.category_id
  422. left join tms_app_loanmerchantbatch talmb on talmb.loan_id = lal.id
  423. left join remaining_principal rp on rp.id = lal.id
  424. left join late_fees lf on lf.id = lal.id
  425. --left join accrued_interest ai on ai.id = lal.id
  426. left join undue_interest ui on ui.loan = lal.id
  427. left join marginalized_interest mi on mi.loan_id = lal.id
  428. left join decomposed_interests di on di.loan_id = lal.id
  429. where lal.loan_booking_day <= {'date':'date'} -- and lal.id = 82651
  430. and lds.blnk_status in (1,2,3,4,5,7,9,10,11,14)
  431. order by lal.id
  432. )
  433.  
  434. select * from main_query
  435. -- where id=70956
  436.  
  437. -- where id=56112
  438.  
  439. -- where id=64241
  440.  
  441. -- where id=94804
  442. -- where id in (84682,85806,99194,100129,102323,102391,102439,104335,124645,93773,94804)
  443.  
  444.  
  445.  
  446.  
  447.  
  448. --where enr <> 0 and total_late_fees <> 0
Success #stdin #stdout #stderr 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 3: unrecognized token: "{"