如何使用sql server查询前8个季度的季度数据报表?

Posted

技术标签:

【中文标题】如何使用sql server查询前8个季度的季度数据报表?【英文标题】:How to query previous 8 quarters for quarterly data report using sql server? 【发布时间】:2020-12-08 06:21:29 【问题描述】:

我想查询从今天开始的前 8 个季度。

示例:从今天开始的最后一个季度 = '2020-09-30',从今天开始的最后 8 个季度是'2018-10-01'。

我希望在查询结果中将前 8 个季度 ('2018-10-01') 标记为 Q1 而不是 Q4,因为它是 2018 年的第四季度。Q2 将是从 1 月开始的下一个季度- 2019 年 3 月等等。

有没有办法从开始日期算到当前日期?

我当前的查询:

    SELECT
       name, 
       Q1, 
       Q2, 
       Q3, 
       Q4, 
       Q5, 
       Q6, 
       Q7, 
       Q8, 
FROM   (SELECT distinct
               s.custId,
               sum(s.total) as total,
               CAST('Q'+ Cast(DATEPART(QUARTER, s.purchaseDate)AS VARCHAR(1)) AS VARCHAR(2)) AS Quarterly,
               c.name, 
        FROM   sales s 
               LEFT OUTER JOIN customers c 
                            ON c.id = s.custId
        WHERE  AND purchaseDate >= '2018-10-01' 
               AND purchaseDate <= '2020-09-30' 
        GROUP BY 
                s.custId) AS data
       PIVOT ( SUM(total) 
             FOR quarterly IN ([Q1], 
                               [Q2], 
                               [Q3], 
                               [Q4], 
                               [Q5], 
                               [Q6], 
                               [Q7], 
                               [Q8]) )AS pvt

       ORDER by name

【问题讨论】:

【参考方案1】:

我只会使用条件聚合:

SELECT s.custId, c.name,
       SUM(CASE WHEN DATEDIFF(quarter, s.purchasedate, GETDATE()) = 8
                THEN s.total
           END) as q1,
       SUM(CASE WHEN DATEDIFF(quarter, s.purchasedate, GETDATE()) = 7
                THEN s.total
           END) as q2,
       . . .
FROM sales s LEFT OUTER JOIN
     customers c 
     ON c.id = s.custId AND
WHERE purchaseDate >= '2018-10-01' AND
      purchaseDate <= '2020-09-30' 
GROUP BY s.custId, c.name;

【讨论】:

【参考方案2】:

对于“硬编码”方法,您可以将 Quarterly 定义为 9-quarterdiff(between purchaseDate&current date) [或 9+quarterdiff(between current date&purchaseDate]

declare @sales table (purchaseDate date, total int);

insert into @sales(purchaseDate, total)
values
('20181016', 4), ('20181220', 5), 
('20190219', 6), 
('20190524', 11), ('20190620', 7), 
('20190708', 12),
('20200210', 20),
('20200923', 19), ('20200926', 11),
('20201111', 2) --this is q9
;


--9&quarterdiff (when/if checking !!previous 8 quarters only!!)
select pvt.*
from
(
select total, 
  concat('Q', 9-datediff(quarter, purchaseDate, getdate())) as Quarterly
  --concat('Q', 9+datediff(quarter, getdate(), purchaseDate)) as Quarterly
from @sales
where purchaseDate >= '20181001' 
and purchaseDate <= '20200930' -- or maybe.. < '20201001', even if it's a date datatype
) as s
pivot
(
sum(total) for Quarterly in (Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8)
) as pvt;

【讨论】:

以上是关于如何使用sql server查询前8个季度的季度数据报表?的主要内容,如果未能解决你的问题,请参考以下文章

MS SQL Server查询 本日本周本月本季度本年起始时间

SQL Server如何查找季度、半年和当年的最后一天?

mysql 按年度季度月度周日SQL统计查询

SQL 2005 查询本周/本月/本季度/本年的数据

mysql按年度季度月度周日统计查询的sql语句

Sql Server中查询当天,最近三天,本周,本月,最近一个月,本季度的数据的sql语句