MS SQL的每日,每周和每月报告

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MS SQL的每日,每周和每月报告相关的知识,希望对你有一定的参考价值。

我想从sql-server表中获取每日,每周和每月报告。

表结构如下:

------------------------------------------
| ItemID      | CommentDate              |
-----------------------------------------
|989898797    | 2019-04-01 02:51:11.153  |
|----------------------------------------|
|989898797    | 2019-04-01 02:51:11.153  |
|----------------------------------------|
|989898797    | 2019-04-03 02:51:11.153  |
|----------------------------------------|
|989898797    | 2019-04-09 02:51:11.153  |
|----------------------------------------|
|989898797    | 2019-04-11 02:51:11.153  |
|----------------------------------------|

到目前为止,我尝试过以下方法,

select (select count(itemid) from ebayfeedback where ((year(commentdate) = year(getdate())) 
and datepart(m,commentdate)=datepart(m,dateadd(month,-1,getdate())))) as lastmonth,

(select count(itemid) from ebayfeedback where (year(commentdate) = year(getdate())) 
and datepart(m,commentdate)=datepart(m,dateadd(month,0,getdate()))) as thismonth,

(select count(itemid) from ebayfeedback where (year(commentdate) = year(getdate())) 
and datepart(wk,commentdate)=datepart(wk,dateadd(week,1,getdate()))) as lastweek,

(select count(itemid) from ebayfeedback where (year(commentdate) = year(getdate())) 
and datepart(wk,commentdate)=datepart(wk,getdate())  group by datepart(wk,commentdate) ) as thisweek,

(select count(itemid) from ebayfeedback where convert(varchar,commentdate,101)= 
convert(varchar,dateadd(day,-1,getdate()),101)) as yesterday,

(select count(itemid) from ebayfeedback where convert(varchar,commentdate,101)= 
convert(varchar,getdate(),101) ) as today

from ebayfeedback

我从上面的查询中收到多行的结果如下所示。

---------------------------------------------------------
| lastmonth   | thismonth | lastweek | thisweek | today |
---------------------------------------------------------
|5            |    5      |    2     |   2      |  1    |
|-------------------------------------------------------|
|5            |    5      |    2     |   2      |  1    |
|-------------------------------------------------------|
|5            |    5      |    2     |   2      |  1    |
|-------------------------------------------------------|
|5            |    5      |    2     |   2      |  1    |
|-------------------------------------------------------|
|5            |    5      |    2     |   2      |  1    |
|-------------------------------------------------------|

我想要每个时期只有一行(1个结果)。请告诉我如何实现这一目标。除了我使用的方法之外,还有什么最好的方法。

期望的结果应该是如下所示的一行。


| lastmonth   | thismonth | lastweek | thisweek | today |
---------------------------------------------------------
|5            |    5      |    2     |   2      |  1    |
|-------------------------------------------------------|

注意:我在上面两个表中的示例中给出的数据不是我的实际数据。

答案

解决此问题的一种方法是使用条件聚合查询来基于它创建报告。

首先,创建并填充样本表(请在以后的问题中保存此步骤):

DECLARE @T AS TABLE
(
    ItemID int,
    CommentDate datetime
);

INSERT INTO @T (ItemId, CommentDate) VALUES
(989898797, '2019-04-01T02:51:11.153'),
(989898797, '2019-04-01T02:51:11.153'),
(989898797, '2019-04-03T02:51:11.153'),
(989898797, '2019-04-09T02:51:11.153'),
(989898797, '2019-04-11T02:51:11.153');

声明局部变量以使查询更具可读性并防止某些代码重复:

DECLARE @Today date = GETDATE(), -- today's date
        @ThisWeek date = DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), GETDATE()), -- first date of the week
        @ThisMonth date = DATEADD(DAY, 1 - DATEPART(DAY, GETDATE()), GETDATE()); -- first date of the month
DECLARE @LastMonth date = DATEADD(MONTH, -1, @ThisMonth), -- first date of last month
        @LastWeek date = DATEADD(WEEK, -1, @ThisWeek) -- first date of last week

查询:

SELECT  COUNT(CASE WHEN CommentDate >= @LastMonth AND CommentDate < @ThisMonth THEN ItemId END) As LastMonth,
        COUNT(CASE WHEN CommentDate >= @ThisMonth AND CommentDate < DATEADD(MONTH, 1, @ThisMonth) THEN ItemId END) As thismonth,
        COUNT(CASE WHEN CommentDate >= @LastWeek AND CommentDate < @ThisWeek THEN ItemId END) As lastweek,
        COUNT(CASE WHEN CommentDate >= @ThisWeek AND CommentDate < DATEADD(WEEK, 1, @ThisWeek) THEN ItemId END) As thisweek,
        COUNT(CASE WHEN CommentDate >= @Today AND CommentDate < DATEADD(DAY, 1, @Today) THEN ItemId END) As today
FROM @T

结果:

LastMonth   thismonth   lastweek    thisweek    today
0           5           2           0           0

以上是关于MS SQL的每日,每周和每月报告的主要内容,如果未能解决你的问题,请参考以下文章

将每日股票价格变成每周/每月/每季度/学期/每年?

Google Analytics API 上的每日/每周/每月活跃用户

每日、每周、每月套餐的价格选项

如何添加报表服务中没有匹配的列(SSRS)

报告以列出新老用户的每日、每月或每年访问

为报告编写 SQL 查询有何不同?