0

我想做这样的事情:

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 中是否可行,正确的语法是什么?

4

2 回答 2

2

检查这个 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
于 2014-03-27T09:52:25.710 回答
0

您的语法无效,因为 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 只需应用一次。)

于 2014-03-27T09:35:39.503 回答