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 :

SELECT doc.client, doc.template, COUNT(doc) FROM document doc GROUP BY doc.client, doc.template

and a table document with the following data :

ID | name  | client | template
1  | doc_a | a      | temp_a
2  | doc_b | a      | temp_a
3  | doc_c | a      | temp_a
4  | doc_d | a      | temp_b

The result for the query would be :

client | template | count
a      | temp_a   | 3
a      | temp_b   | 1

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

client | template | count
a      | temp_a   | 2
a      | temp_a   | 1
a      | temp_b   | 1

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.

WITH 
  GRP_RESULT (client, template, count) AS
(
-- Place your SELECT ... GROUP BY here
-- instead of VALUES 
VALUES
  ('a', 'temp_a', 4500)
, ('a', 'temp_b', 3001)
)
, T (client, template, count, max_batch_size) AS 
(
SELECT client, template, count, 1000
FROM GRP_RESULT
  UNION ALL
SELECT client, template, count - max_batch_size, max_batch_size
FROM T
WHERE count > max_batch_size
)
SELECT client, template, CASE WHEN count > max_batch_size THEN max_batch_size ELSE count END count
FROM T
ORDER BY client, template, count DESC

The result is:

|CLIENT|TEMPLATE|COUNT      |
|------|--------|-----------|
|a     |temp_a  |1000       |
|a     |temp_a  |1000       |
|a     |temp_a  |1000       |
|a     |temp_a  |1000       |
|a     |temp_a  |500        |
|a     |temp_b  |1000       |
|a     |temp_b  |1000       |
|a     |temp_b  |1000       |
|a     |temp_b  |1          |

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