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

时间:2019-04-12 06:38:01

标签: sql-server-2008

我想从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    |
|-------------------------------------------------------|

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

1 个答案:

答案 0 :(得分:0)

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

首先,创建并填充示例表(保存,这是您将来遇到的问题中的这一步):

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