Skip to content
Advertisement

SQL DB2 Split result of group by based on count

I would like to split the result of a group by in several rows based on a count, but I don’t know if it’s possible. For instance, if I have a query like this :

and a table document with the following data :

The result for the query would be :

But I would like to split a row of the result in two or more if the count is higher than 2 :

Is there a way to do this in SQL ?

Advertisement

Answer

You can use RCTE like below. Run this statement AS IS first playing with different values in the last column. Max batch size here is 1000.

The result is:

You may place your SELECT ... GROUP BY statement as specified above afterwards to achieve your goal.

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