Question

I would like to do something like this:

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

where the second column only returns the sum of the total time out hours for the Monday. Is this possible in Oracle SQL, and what is the correct syntax?

Was it helpful?

Solution

check this 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

OTHER TIPS

Your syntax is invalid, because sum belongs to over, but you moved the sum keyword to the beginning of the expression. Here is the corrected statement:

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;

(I also changed places for sum and nvl in your first expression. It does the same but might be nanoseconds faster, because nvl has to be applied just once.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top