Guest User

Untitled

a guest
Jan 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. select
  2. cus.tin,
  3. lt.sum_portfolio as portfolio,
  4. case when (sum(lt.sum_portfolio) over())=0 then null
  5. else (round(100*lt.sum_portfolio/sum(lt.sum_portfolio) over(), 2) || '%') end as "%"
  6. from tbl_customer as cus
  7. inner join (select sum(
  8. case when type = 'loan' then amount
  9. when type = 'loan_repayment' then -1*amount
  10. when type = 'interest' then amount
  11. when type = 'interest_repayment' then -1*amount
  12. end
  13. ) as sum_portfolio,
  14. customer_id
  15. from tbl_loan_transaction
  16. group by customer_id
  17. ) lt on (cus.id = lt.customer_id)
  18. where lt.sum_portfolio is not null
  19. group by cus.tin, lt.sum_portfolio
  20. order by lt.sum_portfolio desc;
Add Comment
Please, Sign In to add comment