Skip to content
Advertisement

sum values based on 7-day cycle in SQL Oracle

I have dates and some value, I would like to sum values within 7-day cycle starting from the first date.

this is my input data with 4 groups to see what groups will create the 7-day cycle. It should start with first date and sum all values within 7 days after first date included. then start a new group with next day plus anothe 7 days, 10-01 till 17-01 and then again new group from 18-01 till 25-01 and so on. so the output will be

with match_recognize would be easy current_day < first_day + 7 as a condition for the pattern but please don’t use match_recognize clause as solution !!!

Advertisement

Answer

One approach is a recursive CTE:

This identifies the groups by the first date. You can use row_number() over (order by firstdte) if you want a number.

Here is a db<>fiddle.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement