动态过滤日期并将总计添加到表的最后一行

时间:2019-05-22 15:53:05

标签: sql-server

我正在尝试通过添加仅将当前年份到前7年的值相加的过滤器来使代码动态化,因此如果在我的示例中年份为2019。然后按顺序显示2012年至2019年。

我还试图将总计添加到Dec之后的最后一列的输出数据中,因此在下面的示例中,2012年以下的值行将在Month Dec之后增加一列,其中Jan到Dec值的总和为是8435 + 31 + 18 = 8484

Year    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
   2012 0   0   0   0   0   0   0   0   0   8435 31 18
   2013 27  21  30  12  31  17  21  9   19  25  18  17
   2014 31  21  16  18  23  31  19  21  12  24  13  15
   2015 20  14  44  37  23  13  37  26  32  57  25  19
   2016 10  15  12  11  18  12  16  44  27  41  29  12
   2017 15  10  12  13  10  13  21  26  11  18  14  7
   2018 9   15  9   29  10  12  12  19  13  20  14  5
   2019 15  10  10  12  5   0   0   0   0   0   0   0

(
year(db.org.DateCreated) ='2012'
or year(db.org.DateCreated) ='2013'
or year(db.org.DateCreated) ='2014'
or year(db.org.DateCreated) ='2015'
or year(db.org.DateCreated) ='2016'
or year(db.org.DateCreated) ='2017'
or year(db.org.DateCreated) ='2018'
or year(db.org.DateCreated) ='2019'
)

select year(db.org.DateCreated) [Year], 
sum(case when month(db.org.DateCreated) = 1 then 1 else 0 end) Jan,
sum(case when month(db.org.DateCreated) = 2 then 1 else 0 end) Feb,
sum(case when month(db.org.DateCreated) = 3 then 1 else 0 end) Mar,
sum(case when month(db.org.DateCreated) = 4 then 1 else 0 end) Apr,
sum(case when month(db.org.DateCreated) = 5 then 1 else 0 end) May,
sum(case when month(db.org.DateCreated) = 6 then 1 else 0 end) Jun,
sum(case when month(db.org.DateCreated) = 7 then 1 else 0 end) Jul,
sum(case when month(db.org.DateCreated) = 8 then 1 else 0 end) Aug,
sum(case when month(db.org.DateCreated) = 9 then 1 else 0 end) Sep,
sum(case when month(db.org.DateCreated) = 10 then 1 else 0 end) Oct,
sum(case when month(db.org.DateCreated) = 11 then 1 else 0 end) Nov,
sum(case when month(db.org.DateCreated) = 12 then 1 else 0 end) Dec

1 个答案:

答案 0 :(得分:0)

一种选择是使用CTE生成每年的每月总计,然后查询该CTE以查找跨月的总和。

WITH cte AS (
    SELECT
        YEAR(db.org.DateCreated) [Year],
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 1 THEN 1 END) Jan,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 2 THEN 1 END) Feb,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 3 THEN 1 END) Mar,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 4 THEN 1 END) Apr,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 5 THEN 1 END) May,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 6 THEN 1 END) Jun,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 7 THEN 1 END) Jul,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 8 THEN 1 END) Aug,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 9 THEN 1 END) Sep,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 10 THEN 1 END) Oct,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 11 THEN 1 END) Nov,
        COUNT(CASE WHEN MONTH(db.org.DateCreated) = 12 THEN 1 END) Dec
    WHERE YEAR(GETDATE()) - YEAR(db.org.DateCreated) BETWEEN 0 AND 7
    FROM yourTable
    GROUP BY
        YEAR(db.org.DateCreated)
)

SELECT
    Year,
    Jan,
    Feb,
    Mar,
    Apr,
    May,
    Jun,
    Jul,
    Aug,
    Sep,
    Oct,
    Nov,
    Dec,
    (Jan + Feb + Mar + Apr + May + Jun + Jul + Aug + Sep + Oct + Nov + Dec) AS total
FROM cte
ORDER BY
    year;

请注意,我将您对SUM的呼叫替换为COUNT,这更加简洁易读。