Skip to content

Instantly share code, notes, and snippets.

@feldwebel
Last active October 16, 2017 12:19
Show Gist options
  • Select an option

  • Save feldwebel/0cdd1a548476cdd604316d849ecd8bd5 to your computer and use it in GitHub Desktop.

Select an option

Save feldwebel/0cdd1a548476cdd604316d849ecd8bd5 to your computer and use it in GitHub Desktop.
Percentage
select c.tin, sum(case when lt.type = any(array['interest', 'loan']) then lt.amount else -lt.amount end) portfolio,
round(
100.0
* sum(case when lt.type = any(array['interest', 'loan']) then lt.amount else -lt.amount end)
/ (select sum(case when type = any(array['interest', 'loan']) then amount else -amount end) from tbl_loan_transaction)
, 2) "%"
from tbl_loan_transaction lt
join tbl_customer c on c.id = lt.customer_id
group by c.tin
having sum(case when lt.type = any(array['interest', 'loan']) then lt.amount else -lt.amount end) > 0
order by portfolio desc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment