简体   繁体   English

SQL DB2 根据计数拆分分组结果

[英]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 :但如果计数高于 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 ?有没有办法在 SQL 中做到这一点?

You can use window functions and then aggregate:您可以使用窗口函数,然后聚合:

SELECT client, template, COUNT(*)
FROM (SELECT doc.client, doc.template,
             ROW_NUMBER() OVER (PARTITION BY doc.client, doc.template ORDER BY doc.client) - 1 as seqnum,
             COUNT(*) OVER (PARTITION BY doc.client, doc.template) as cnt
      FROM document doc
     ) d
GROUP BY doc.client, doc.template, floor(seqnum * n / cnt)

The subquery enumerates the rows.子查询枚举行。 The outer query then splits the rows into groups of two using MOD() .然后外部查询使用MOD()将行分成两组。

You can use RCTE like below.您可以像下面那样使用 RCTE。 Run this statement AS IS first playing with different values in the last column.首先按原样运行此语句,在最后一列中使用不同的值。 Max batch size here is 1000.此处的最大批量大小为 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.您可以在之后放置上面指定的SELECT ... GROUP BY语句以实现您的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM