我们如何在每日报告的同一行中获取今天的值和最近7天的值?

时间:2019-05-03 13:33:15

标签: sql sql-server-2008-r2

我在SQL Server 2008 R2中有一个表,其中包含3列NameDateToday's values。我可以轻松地在报告中显示,但我们也需要在同一报告中查看“过去7天总计”值。我的意思是每一行都应像名称,日期,“今天的价值”和“过去7天总计”一样表示。

请问您需要帮助吗?

2 个答案:

答案 0 :(得分:1)

SELECT 
    t.Name, t.Date, t.Value, 
    (SELECT SUM(s.Value) 
     FROM your_table s 
     WHERE s.date <= t.date 
       AND s.date > DATEADD(DAY, -7, t.date
    ) AS last_7day_value
FROM 
    your_table t
WHERE 
    t.date BETWEEN @start_date AND @end_date;

答案 1 :(得分:0)

由于您不能使用排序的窗口聚合来生成总计,因此可以通过自selectjoin来实现子cross apply或非等分逻辑,而无需这样做:

declare @t table(ValueName varchar(10),ValueDate date, ValueAmount int);
insert into @t values
 ('Name 1','20190101',4)
,('Name 1','20190102',3)
,('Name 1','20190103',8)
,('Name 1','20190104',6)
,('Name 1','20190105',9)
,('Name 1','20190106',6)
,('Name 1','20190107',3)
,('Name 1','20190108',2)
,('Name 1','20190109',1)
,('Name 2','20190101',3)
,('Name 2','20190102',7)
,('Name 2','20190103',6)
,('Name 2','20190104',8)
,('Name 2','20190105',8)
,('Name 2','20190106',7)
,('Name 2','20190107',6)
,('Name 2','20190108',9)
,('Name 2','20190109',3);

select t.ValueName
      ,t.ValueDate
      ,t.ValueAmount
      ,t.ValueAmount + isnull(sum(p.ValueAmount),0) as SevenDayTotal
from @t as t
    cross apply(values(dateadd(day,-1,t.ValueDate))
                     ,(dateadd(day,-2,t.ValueDate))
                     ,(dateadd(day,-3,t.ValueDate))
                     ,(dateadd(day,-4,t.ValueDate))
                     ,(dateadd(day,-5,t.ValueDate))
                     ,(dateadd(day,-6,t.ValueDate))
                     ) as d(d)
    left join @t as p
        on t.ValueName = p.ValueName
            and p.ValueDate = d.d
group by t.ValueName
      ,t.ValueDate
      ,t.ValueAmount
order by t.ValueName
      ,t.ValueDate;

输出

+-----------+------------+-------------+---------------+
| ValueName | ValueDate  | ValueAmount | SevenDayTotal |
+-----------+------------+-------------+---------------+
| Name 1    | 2019-01-01 |           4 |             4 |
| Name 1    | 2019-01-02 |           3 |             7 |
| Name 1    | 2019-01-03 |           8 |            15 |
| Name 1    | 2019-01-04 |           6 |            21 |
| Name 1    | 2019-01-05 |           9 |            30 |
| Name 1    | 2019-01-06 |           6 |            36 |
| Name 1    | 2019-01-07 |           3 |            39 |
| Name 1    | 2019-01-08 |           2 |            37 |
| Name 1    | 2019-01-09 |           1 |            35 |
| Name 2    | 2019-01-01 |           3 |             3 |
| Name 2    | 2019-01-02 |           7 |            10 |
| Name 2    | 2019-01-03 |           6 |            16 |
| Name 2    | 2019-01-04 |           8 |            24 |
| Name 2    | 2019-01-05 |           8 |            32 |
| Name 2    | 2019-01-06 |           7 |            39 |
| Name 2    | 2019-01-07 |           6 |            45 |
| Name 2    | 2019-01-08 |           9 |            51 |
| Name 2    | 2019-01-09 |           3 |            47 |
+-----------+------------+-------------+---------------+