【问题标题】:how to calculate count of mode with aggregation functions in oracle如何在 oracle 中使用聚合函数计算模式计数
【发布时间】:2015-01-29 14:07:43
【问题描述】:

如何使用 oracle 中的聚合函数计算 模式计数

我的意思是这样的:

我的数据:

table1:
CITY        JOB
++++++++++++++++++++++
Toronto     Programmer
Vancouver   Manager
Vancouver   Manager
Vancouver   Tester
Vancouver   Tester
Vancouver   Tester
Vancouver   Tester
New York    Manager
New York    Manager
New York    Manager
New York    Tester

我的愿望查询:

 select city,  
        stats_mode(job) sm,  
        x(job) count_of_stats_mode
 from table1

结果:

city       sm           count_of_stats_mode
++++++++++++++++++++++++++++++++++++++++++++++++++
Toronto    Programmer   1
Vancouver  Tester       4
New York   Manager      3

我需要 x 这是一个函数
只是我不想使用子查询

【问题讨论】:

  • 为什么不想使用子查询?

标签: sql oracle oracle-sqldeveloper analysis


【解决方案1】:

我想不出没有子查询的方法:

select city, job, cnt
from (select city, job, count(*) as cnt,
             row_number() over (partition by city order by count(*) desc) as seqnum
      from table1
      group by city, job
     ) t
where seqnum = 1;

编辑:

这并不漂亮,但我认为这可以在没有子查询的情况下满足您的要求:

select distinct city,
       max(job) keep (dense_rank first order by count(*) desc) over (partition by city) as mode_job,
       max(count(*)) over (partition by city) as mode_cnt
from table1
group by city, job;

【讨论】:

  • 谢谢,但这不是答案。我考虑“从...中选择计数(stats_mode(job)=job然后1结束时的情况)”,但不允许嵌套分组。
  • @parvij 。 . .第二个查询 is 是对您的问题的回答,它在没有子查询的情况下获取模式和计数。您评论中的版本无效。
【解决方案2】:
SELECT CITY, JOB, COUNT(CITY) AS count_of_stats_mode
FROM table1
GROUP BY CITY, JOB

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多