按组计算百分比

时间:2016-11-22 21:28:44

标签: sql oracle group-by percentage

我需要计算一组组合的百分比,我有类似的东西

c1     c2    Month     Sol         Amnt

---     ---   ------   ------     -------
AA     BA   April       F           123
AA     BA   April       A           45
AA     BA   April       N           576
AB     AS   April       F           12
AB     AS   April       A           20
AA     BA   March       F           34
AA     BA   March       N           15
AA     BA   March       A           10

我需要输出像这样

c1      c2     Month   Sol        Amnt      Total      %

----  ----   -------    ----       ------     ------    -------
AA     BA     April      F          123        744       16.53%
AA     BA     April      A          45         744       6.05%
AA     BA     April      N          576        744       77.42%
AB     AS     April      F          12         32        37.50%
AB     AS     April      A          20         32        62.50%
AC     BA     March      F          34         59        57.63%
AC     BA     March      N          15         59        25.42%
AC     BA     March      A          10         59        16.95%

4 个答案:

答案 0 :(得分:0)

CREATE TABLE TBL1
(
  c1 VARCHAR(20)
, c2 VARCHAR(20) 
, Month VARCHAR(20) 
, Sol VARCHAR(20) 
, Amnt integer 
);


   insert INTO tbl1 (c1,c2,Month,Sol,Amnt) values ('AA','BA','April','F',123);
   insert INTO tbl1 (c1,c2,Month,Sol,Amnt) values ('AA','BA','April','A',45);
   insert INTO tbl1 (c1,c2,Month,Sol,Amnt) values ('AA','BA','April','N',576);
   insert INTO tbl1 (c1,c2,Month,Sol,Amnt) values ('AB','AS','April','F',12);
   insert INTO tbl1 (c1,c2,Month,Sol,Amnt) values ('AB','AS','April','A',20);
   insert INTO tbl1 (c1,c2,Month,Sol,Amnt) values ('AC','BA','March','F',34);
   insert INTO tbl1 (c1,c2,Month,Sol,Amnt) values ('AC','BA','March','N',15);
   insert INTO tbl1 (c1,c2,Month,Sol,Amnt) values ('AC','BA','March','A',10);



SELECT
  Tbl1.*
  ,Grp.ttlAmt
  ,Tbl1.Amnt / Grp.ttlAmt * 100
FROM
  TBL1 
  left join ( 
  select c1 , sum(amnt) as ttlAmt from Tbl1 group by c1)Grp
  on Grp.c1 = Tbl1.C1

添加了SQLfiddle链接 http://sqlfiddle.com/#!2/0ac79/2

答案 1 :(得分:0)

将此添加到您的查询中:

TO_CHAR((amnt/total)*100||'%' as "%"

答案 2 :(得分:0)

您可以使用按不同聚合级别

分组的相同表格
select c1, c2, Month, Sol, Tot_Sol_Amnt, Tot_Amnt, (Tot_Sol_Amnt/Tot_Amnt )*100 as perc 
from (
 select t2.c1 as c1, t2.c2 as c2, t2.Month as Month, t2.Sol as Sol, sum(t2.Amnt) as Tot_Sol_Amnt
      from tbl1 t2
      group by  t2.c1, t2.c2, t2.Month, t2.Sol
) tt1 inner join (
 select t1.c1, t1.c2, t1.Month, sum(t1.Amnt) Tot_Amnt
        from tbl1 t1
        group by  t1.c1, t1.c2, t1.Month
) tt2  on tt1.c1  = tt2.c1 AND tt1.c2  = tt2.c2 AND tt1.month  = tt2.month

答案 3 :(得分:0)

您可以使用分析函数按组计算值。

select c1, c2, month, sol, Amnt
, sum(amnt) over(partition by c1,c2, month order by c1, c2) Total
, round((amnt / sum(amnt) over(partition by c1,c2, month order by c1,c2))*100,2)||'%' "%"
from TBL1;

魔力部分结束(分区... 应用于任何聚合。

  

总和(amnt)结束(按c1,c2划分,按c1,c2划分月份)总计

结果: enter image description here