DB2,用group by语句数行

时间:2019-01-08 05:48:24

标签: sql db2

我在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

哪个可以正常工作,当前返回的内容如下:

current_year | prior_year  |  full_year
---------------------------------------
412             562             613

问题是,我想按某些条件对它们进行分组,但是它不再返回一个结果集,而是返回了许多具有各自计数的结果集:

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

因此,我希望对分组进行计数(而不是每行计数)(基本上是降低每个数字/计数,但仍然仅是结果集)

我是否缺少正确执行此操作的功能?

更新:

一个例子-

如果我有以下行

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'

因此,如果它们都在某个日期范围内(例如,previous_year),那么我希望该计数为3而不是10

如果我做了类似上述的选择

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

我希望:

lastYear
--------
   3

1 个答案:

答案 0 :(得分:1)

我们可以在此处尝试两次汇总,一次找到符合您要求的分组,第二次汇总所有分组的总数。

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;