【问题标题】:How to use a case or decode as part of an analytical window function in Oracle SQL如何在 Oracle SQL 中使用案例或解码作为分析窗口函数的一部分
【发布时间】:2014-03-27 09:16:47
【问题描述】:

我想做这样的事情:

select sum(nvl(total_time_out, 0)),
       sum(nvl((case when day_of_week = 'Mon' then total_time_out else 0 end) over (partition by person_id), 0))
from   xxpay_tna_summary_v
where  person_id = 7926

其中第二列仅返回星期一的总超时小时数之和。这在 Oracle SQL 中是否可行,正确的语法是什么?

【问题讨论】:

  • 如果有人要解决这个问题,你必须提供更多细节......

标签: sql oracle analytic-functions


【解决方案1】:

检查这个 http://sqlfiddle.com/#!4/df376/2

select sum((case when person_id = 100 then total_time_out else 0 end)) total_time,
   sum(nvl((case when day_of_week = 'MON' then total_time_out else 0 end), 0)) monday_time
from   xxpay_tna_summary_v

【讨论】:

    【解决方案2】:

    您的语法无效,因为 sum 属于 over,但是您将 sum 关键字移到了表达式的开头。以下是更正后的陈述:

    select nvl(sum(total_time_out), 0),
           nvl(sum(case when day_of_week = 'Mon' then total_time_out else 0 end) over (partition by person_id), 0)
    from   xxpay_tna_summary_v
    where  person_id = 7926;
    

    (我还在您的第一个表达式中更改了 sum 和 nvl 的位置。它的作用相同,但可能快纳秒,因为 nvl 只需应用一次。)

    【讨论】:

    • 这可能会因not a single-group group function 而失败,因为第一个 SUM 是聚合函数。
    • 您的选择因非单组组功能而失败。
    猜你喜欢
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    相关资源
    最近更新 更多