使用汇总汇总运行总计,并为“ 1”分配最大分组总数,同样,为“ 2”为第二个最大分组分组

时间:2019-04-22 06:39:22

标签: oracle11g

使用“汇总”添加运行总计,然后将“ 1”分配为最大分组方式,同样,将“ 2”分配为第二个最大分组方式

假设,存款人将拥有多种类型的帐户(储蓄,活期,定期),并具有各自的金额值 假设我们的“总臭虫总数” = 5000,  占总数的百分比=(总数/总数)

`Name of the Depositor`  `Types of Deposits` `Amount`  `% of total depostits`

       A                saving               10          0.002
                        current              20          0.004 
                        Term                 30          0.006
                        Total                60          0.012

       B                Saving               50          0.01
                        term                 60          0.012
                        Total                110         0.022

我希望根据“总坏账率”的最大“总数”来确定前20名存款人。

WITH cte AS (
select c.cust_id ,
c.cust_name AS C2,
CASE WHEN B.SCHM_TYPE ='SBA' THEN 'Savings' WHEN B.SCHM_TYPE ='CAA' THEN 'Current' WHEN B.SCHM_TYPE ='TDA' THEN 'Term' ELSE 'Total' END AS C3,
Round(SUM(eod_bal_amt),2) AS C4,
((SUM(eod_bal_amt))*100/(select Nvl(SUM(eod_bal_amt),0) AS C7 
from a,b where A.acid=B.ACID
AND B.SCHM_TYPE IN('SBA','TDA','CAA','SDA')
AND 20190331 BETWEEN B.FROM_DATE_SK AND B.TO_DATE_SK 
AND 20190331 BETWEEN A.FROM_DATE_SK AND A.TO_DATE_SK)) AS C5,
Dense_Rank () OVER (order BY c.cust_id) dn

from b JOIN fct_eod_bal_etl_temp a ON a.ACid=B.ACID
JOIN c ON c.CUST_ID=B.CUST_ID
AND B.SCHM_TYPE IN('SBA','TDA','CAA')
AND 20190331 BETWEEN B.FROM_DATE_SK AND B.TO_DATE_SK
AND 20190331 BETWEEN A.FROM_DATE_SK AND A.TO_DATE_SK
AND 20190331 BETWEEN C.FROM_DATE_SK AND C.TO_DATE_SK
GROUP BY c.cust_name,B.SCHM_TYPE,c.cust_id
)
SELECT cust_id,c2,nvl(C3,'Total') AS C3,Sum(C4) AS C4,Sum(c5) as C5,dn
FROM cte
group by cust_id,c2,rollup (c3),dn

op应该是拥有最大“总depostits的%”的存款人 将为整个客户组分配为“ 1”。

Name ofDepositor Types of Deposits Amount %total_depostits new

  A                 saving               10          0.002        2
                    current              20          0.004        2 
                    Term                 30          0.006        2
                    Total                60          0.012        2

   B                Saving               50          0.01         1
                    term                 60          0.012        1
                    Total                110         0.022        1

0 个答案:

没有答案