[英]How to use a case or decode as part of an analytical window function in Oracle SQL
我想做這樣的事情:
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中是否可行,並且正確的語法是什么?
請查看此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
您的語法無效,因為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只需要應用一次。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.