任何人都可以帮我计算一个查询的%份额。任何帮助表示赞赏。
%分享计算:这是我们计算%份额所需的方式。 拥有余额的借款人总数> 500000,所有借款人的余额总和除以所有借款人的总和。
(
(
SELECT SUM(OUTSTANDING_BAL)
FROM MyView
GROUP BY BORROWER_NAME
HAVING SUM(OUTSTANDING_BAL) > 500000 -- SUM OF BORROWER HAVING BALANCE > 500000
)
/ -- DIVIDE BY
(SELECT SUM(OUTSTANDING_BAL) FROM MyView) -- TOTAL SUM
)*100
请帮忙。
提前谢谢。
答案 0 :(得分:0)
如果您只想要一个包含此信息的查询,请使用条件聚合:
select (sum(case when outstanding_bal > 500000 then outstanding_bal else 0 end) /
sum(outstanding_bal)
) as ratio
from (select borrower_name, sum(outstanding_bal) as outstanding_bal
from MyView
group by borrower_name
) b;
这假设借用可能有多个您想要添加的记录。如果您只想考虑与阈值相关的各个行,则查询更简单:
select (sum(case when outstanding_bal > 500000 then outstanding_bal else 0 end) /
sum(outstanding_bal)
) as ratio
from MyView;