【问题标题】:How can i display the maximum/minimum of count() column records in oracle如何在 oracle 中显示 count() 列记录的最大值/最小值
【发布时间】:2015-02-04 23:01:10
【问题描述】:

Oracle 查询

select
  courses.name,
  count(gardener)
from
  attendances
  join courses using (course)
group by
  courses.name
order by
  count(gardener) asc

输出

╔═══════════╦═════════════════╗
║   NAME    ║ COUNT(GARDENER) ║
╠═══════════╬═════════════════╣
║ Harvesting║               3 ║
║ Planting  ║               3 ║
║ Gardening ║               4 ║
╚═══════════╩═════════════════╝

我怎样才能显示最小/最大甚至平均计数。例如。最低限度

// I just want to display the courses with minimum count
╔═══════════╦═════════════════╗
║   NAME    ║ COUNT(GARDENER) ║
╠═══════════╬═════════════════╣
║ Harvesting║               3 ║
║ Planting  ║               3 ║
╚═══════════╩═════════════════╝

我试过了

select
 max(cnt), -- min(cnt) and avg(cnt) doesnt seem to work
 name
from
(
  select
    courses.name,
    count(gardener) as cnt
  from
    attendances
    join courses using (course)
  group by
    courses.name
  order by
    count(gardener) asc
)
group by
  name

我得到无效的标识符“cnt”。我尝试使用 max(cnt) 它也给了我相同的标识符错误,任何人都可以更正我的语法并建议我做错了什么,或者建议一种不同的方法来做到这一点?

编辑:我已经删除了语音标记,但仍然显示所有记录,而不仅仅是我需要的最大值 count() 记录。

【问题讨论】:

    标签: sql oracle11g


    【解决方案1】:

    删除双引号,然后重试

    【讨论】:

    • 谢谢,我删除了语音标记,现在标识符错误消失了。我添加了“select max(cnt), name”+“group by name”,它仍然显示所有其他记录,我只需要显示最高计数值记录。
    【解决方案2】:

    如果你想要最少数量的课程,我会推荐分析函数:

    select name, cnt
    from (select c.name, count(gardener) as cnt,
                 dense_rank() over (order by count(gardener)) as seqnum
          from attendances a join
               courses c
               using (course)
          group by c.name
         ) ca
    where seqnum = 1;
    

    【讨论】:

    • 非常感谢,会调查的。
    猜你喜欢
    • 1970-01-01
    • 2019-09-20
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2010-10-24
    • 1970-01-01
    相关资源
    最近更新 更多