Search code examples
sqloracle-databaseanalytic-functions

How to use a case or decode as part of an analytical window function in Oracle SQL


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?


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