Skip to content
Advertisement

For each period I need to sum the period and previous periods

I need help creating a query . I want to group by Period and Jobid. For each grouping I need to sum the hourworkedtotal for each period. This value will also include any periods prior to this period.

For example MAR20 and Jobid 3391 would have an hoursworkedtotal of 93 as Feb20 hoursworkedtotal will be included.

enter image description here

Advertisement

Answer

You can use window functions with aggregation:

select period, jobid, sum(hoursworkedtotal) as this_month,
       sum(sum(hoursworkedtotal)) over (partition by jobid order by period) as running_hours
from t
group by period, jobid;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement