Skip to content
Advertisement

Group by values that are each multiple of number

This is the table t. I want to group it every time the TotalQty >= 5n (let n = group). i.e. once the TotalQty >= 5n I want to sum together the qty from n-1 to n.

With this query I can get pretty close but I doesn’t consider the the previous “valid” TotalQty + 5


How do I get this result?

And so on, the next group would be until 17+5 = 22

Advertisement

Answer

You need to use a recursive CTE for this:

EDIT:

Hold on. I think the answer is simpler.

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