[英]DB2, several count lines with a group by statement
I have a pretty simple query in db2 that returns 3 different conditional counts, each as a value: 我在db2中有一个非常简单的查询,该查询返回3个不同的条件计数,每个条件计数都是一个值:
SELECT
IFNULL(COUNT(CASE WHEN CURRENT DATE BETWEEN n.start_date AND n.expire_date
THEN 1 END), 0) AS current_year,
IFNULL(COUNT(CASE WHEN CURRENT DATE - 1 YEAR
BETWEEN n.start_date AND n.expire_date
THEN 1 END), 0) AS prior_year,
IFNULL(COUNT(CASE WHEN DATE('2018-12-31') - 7 DAY
BETWEEN n.start_date AND n.expire_date
THEN 1 END), 0) AS full_year
FROM newData n
WHERE n.customer = 111
Which works fine and currently returns something like: 它工作正常,当前返回类似:
current_year | prior_year | full_year
---------------------------------------
412 562 613
The problem is, I want to group them by certain conditions but it no longer returns the one result set, rather a lot of result sets each with their own count: 问题是,我想按特定条件对它们进行分组,但是它不再返回一个结果集,而是返回了许多具有各自计数的结果集:
SELECT
IFNULL(COUNT(CASE WHEN CURRENT DATE BETWEEN n.start_date AND n.expire_date
THEN 1 END), 0) AS current_year,
IFNULL(COUNT(CASE WHEN CURRENT DATE - 1 YEAR
BETWEEN n.start_date AND n.expire_date
THEN 1 END), 0) AS prior_year,
IFNULL(COUNT(CASE WHEN DATE('2018-12-31') - 7 DAY
BETWEEN n.start_date AND n.expire_date
THEN 1 END), 0) AS full_year
FROM newData n
WHERE n.customer = 111
GROUP BY color,category
So rather than a count of every line I'd like to get a count of the groupings (basically lowering each number/count but still only result set) 因此,我不想计数每行,而是希望对分组进行计数(基本上是降低每个数字/计数,但仍然仅是结果集)
Is there something that i'm missing to properly do this with? 我缺少正确执行此操作的内容吗?
UPdate: 更新:
An example - 一个例子 -
If I have the following rows 如果我有以下行
product | color | category | start_date | expire_date
-------------------------------------------------------------
1 Red Leather '2018-12-01' '2018-12-31'
2 Red Leather '2018-12-01' '2018-12-31'
3 Red Leather '2018-12-01' '2018-12-31'
4 Blue Leather '2018-12-01' '2018-12-31'
5 Blue Leather '2018-12-01' '2018-12-31'
6 Blue Leather '2018-12-01' '2018-12-31'
7 Green Leather '2018-12-01' '2018-12-31'
8 Green Leather '2018-12-01' '2018-12-31'
9 Green Leather '2018-12-01' '2018-12-31'
10 Green Leather '2018-12-01' '2018-12-31'
So if they all fall within a certain date range, say prior_year, then I would want that count to be 3 instead of 10 因此,如果它们都在某个日期范围内(例如,previous_year),那么我希望该计数为3而不是10
If I did a select similar to above 如果我做了类似上面的选择
select
ifnull(count(case when '2018'12'15' between start_date and expire_date then 1 end),0) as LastYear
from newData
group by color,category
I would expect: 我期望:
lastYear
--------
3
We can try aggregating twice here, once to find the groups which meet your requirements, and the second to take the total counts over all groups. 我们可以在这里尝试两次汇总,一次找到符合您要求的分组,第二次汇总所有分组的总数。
WITH cte AS (
SELECT
detail1,
detail2,
detail3,
COUNT(*) AS cnt,
COUNT(CASE WHEN CURRENT DATE BETWEEN n.start_date AND n.expire_date
THEN 1 END) AS current_year,
COUNT(CASE WHEN CURRENT DATE - 1 YEAR BETWEEN n.start_date AND n.expire_date
THEN 1 END)AS prior_year,
COUNT(CASE WHEN DATE('2018-12-31') - 7 DAY
BETWEEN n.start_date AND n.expire_date THEN 1 END) AS full_year
FROM newData n
WHERE n.customer = 111
GROUP BY
detail1,
detail2,
detail3
)
SELECT
COUNT(CASE WHEN cnt = current_year THEN 1 END) AS current_year,
COUNT(CASE WHEN cnt = prior_year THEN 1 END) AS prior_year,
COUNT(CASE WHEN cnt = full_year THEN 1 END) AS full_year
FROM cte;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.