【问题标题】:calculating percentage of a value in an oracle database计算oracle数据库中值的百分比
【发布时间】:2014-02-05 19:20:48
【问题描述】:

我想对 oracle 表中的一行进行一些统计。

select totaltime from server_metrics;

给我一​​个数值列表。 我现在想知道的是表中的值有多少百分比高于100?

我可以通过两个查询轻松完成并计算我自己:

select count(*) from server_metrics;
select count(*) from server_metrics where totaltime > 100;

但是可以通过一个查询来做到这一点吗?

【问题讨论】:

    标签: database oracle


    【解决方案1】:
    select 100
             * sum(case when totaltime > 100 then 1 else 0 end)
             / Nullif(count(*),0)
    from server_metrics;
    

    【讨论】:

    • Nullif(count(*),0) = NULL,如果是COUNT(*) = 0。你打算NVL()
    • 通过将 count(*) 的值为零视为 Null 来避免空表上的除零错误。当然,Count(*) 永远不会为空。
    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 2017-10-13
    • 2020-04-17
    相关资源
    最近更新 更多