如何按组分组百分比计算

时间:2015-11-20 17:40:21

标签: sql count group-by percentage

我的数据包含下表:

mytable(国家,性别):

+----------+----------+
| country  | gender   |
+----------+----------+
| China    | male     |
+----------+----------+
| China    | female   |
+----------+----------+
| China    | male     |
+----------+----------+
| China    | male     |
+----------+----------+
| Russia   | male     |
+----------+----------+
| Russia   | female   |
+----------+----------+

我想要一个像这样的查询选择输出:

+----------+----------+--------+-----------+
| country  | gender   | count  | percent   |
+----------+----------+--------+-----------+
| China    | male     |   3    |     75    |
+----------+----------+--------+-----------+
| China    | female   |   1    |     25    |
+----------+----------+--------+-----------+
| Russia   | male     |   1    |    50     |
+----------+----------+--------+-----------+
| Russia   | female   |   1    |    50     |
+----------+----------+--------+-----------+

所以基本上我想计算每个国家性别的百分比。

我该怎么做?

提前多多感谢

4 个答案:

答案 0 :(得分:0)

<强> Sql Fiddle Demo

double

答案 1 :(得分:0)

你可以试试这个.. (在Oracle 11上测试):

  with w_data as (
        select 'China' country, 'male'  gender from dual union all
        select 'China'        , 'female'       from dual union all
        select 'China'        , 'male'         from dual union all
        select 'China'        , 'male'         from dual union all
        select 'Russia'       , 'male'         from dual union all
        select 'Russia'       , 'female'       from dual 
        ),
     w_sub as (
        select country, gender, count(*) ccount
          from w_data
         group by country, gender
        )
  select country, gender, ccount,
         ratio_to_report(ccount) over (partition by country)*100 percent
    from w_sub
  order by country, ccount desc
  /

  COUNTR GENDER     CCOUNT    PERCENT
  ------ ------ ---------- ----------
  China  male            3         75
  China  female          1         25
  Russia female          1         50
  Russia male            1         50

避免&#34; WITH&#34;条款(因为我认为它不适用于某些数据库)...假设RATIO_TO_REPORT有效...... 相同的查询略有不同:

  select country, gender, ccount,
         ratio_to_report(ccount) over (partition by country)*100 percent
    from (
           select country, gender, count(*) ccount
             from (
                    select 'China' country, 'male'  gender from dual union all
                    select 'China'        , 'female'       from dual union all
                    select 'China'        , 'male'         from dual union all
                    select 'China'        , 'male'         from dual union all
                    select 'Russia'       , 'male'         from dual union all
                    select 'Russia'       , 'female'       from dual 
                    )  w_data
            group by country, gender
           )
  order by country, ccount desc
  /

最后,如果你没有RATIO_TO_REPORT,那就避免使用RATIO_TO_REPORT:

  select country, gender, ccount,
         (ccount / SUM(ccount) over (partition by country))*100 percent
    from (
           select country, gender, count(*) ccount
             from (
                    select 'China' country, 'male'  gender from dual union all
                    select 'China'        , 'female'       from dual union all
                    select 'China'        , 'male'         from dual union all
                    select 'China'        , 'male'         from dual union all
                    select 'Russia'       , 'male'         from dual union all
                    select 'Russia'       , 'female'       from dual 
                    )  w_data
            group by country, gender
           )
  order by country, ccount desc
  /

答案 2 :(得分:0)

您可以这样使用:

select a.country, 
       a.gender, 
       (count(*) / b.qtd)*100 as percent
  from test a inner join
       (select country, count(*) qtd from test group by country) b
      on a.country = b.country
 group by a.country, a.gender

不需要两个子查询! 请在此处查看:http://sqlfiddle.com/#!9/f41609/4

答案 3 :(得分:0)

只需对主要查询中的国家/地区和性别进行分组,并使用百分比公式获取每个组中有多少性别的子查询。

select 
  a.country, 
  a.gender,
  count(a.gender) as `count`, 
  (
    select count(a.gender) / count(gender) * 100 
    from mytable 
    where country = a.country
  ) as percent
from mytable a 
group by 
  a.country, 
  a.gender
order by
  country,
  gender desc,
  percent desc