0

我正在使用 SQL Server 2012。我有两个表来保存产品订单。具有接收日期的订单和具有价格和订单 ID fk 的 OrderItem。

我有一个查询,按日期对订单进行分组,并获取该日期的订单数量和总价。我还有一列是 7 天前订单数量的差异。

但是,我还希望能够获得过去 7 天内所有订单的平均值。

所以目前我有以下查询:

declare @DateFrom datetime
set @DateFrom = '2012-12-01'

declare @DateTo datetime
set @DateTo = '2013-03-13'

;with orders as (
    select 
    cast(o.ReceivedDate as date) as OrderDate, 
    count(oi.Id) as Orders,
    coalesce(sum(oi.Price), 0) as Price
    from OrderItem oi 
    join [Order] o on oi.OrderId = o.Id
    where cast(o.ReceivedDate as date) >= @DateFrom 
    and cast(o.ReceivedDate as date) <= @DateTo
    group by cast(o.ReceivedDate as date)
)

select c1.OrderDate,
c1.Price,
c1.Orders,
c1.Orders - c2.Orders as DIFF7DAYS
from orders c1
left join orders c2 on dateadd(day, -7, c1.OrderDate) = c2.OrderDate
order by c1.OrderDate desc

现在我想添加另一列来获取过去 7 天的平均订单。

我试过类似的东西:

select c1.OrderDate,
c1.Price,
c1.Orders,
c1.Orders - c2.Orders as DIFF7DAYS,
c3.AverageOrders
from orders c1
left join orders c2 on dateadd(day, -7, c1.OrderDate) = c2.OrderDate
left join (
select OrderDate, avg(Orders) as AverageOrders
from orders
group by OrderDate
) as c3 on c3.OrderDate >= dateadd(day, -7, c1.OrderDate) and c3.OrderDate <= c1.OrderDate     and c3.OrderDate = c1.OrderDate
order by c1.OrderDate desc

但似乎没有做什么我做什么。我还尝试从连接中删除 c3.OrderDate = c1.OrderDate 但是我得到了影响平均值的重复行。基本上我想在结果中添加一列:

select avg(Orders) as AverageOrders
from orders
where OrderDate >= (the current order - 7 days) and OrderDate <= (the current order)

但我不知道该怎么做?我创建了一个 sqlfiddle 来帮助解释http://sqlfiddle.com/#!6/8b837/44

因此,从我的示例数据中,我想要实现的是这样的结果:

|  ORDERDATE | ORDERS | PRICE |  DIFF7DAYS  | AVERAGE |
-------------------------------------------------------
| 2013-01-25 |      7 |    38 |           6 |       2 |
| 2013-01-24 |      2 |    12 |        null |       1 |
| 2013-01-23 |      1 |    10 |        null |       1 |
| 2013-01-22 |      1 |    33 |        null |
| 2013-01-18 |      1 |    10 |        null |
| 2013-01-10 |      1 |     3 |          -2 |
| 2013-01-08 |      2 |    11 |        null |
| 2013-01-04 |      1 |     1 |        null |
| 2013-01-03 |      3 |    46 |        null |

如您所见,25 日的平均值为 2,因为过去 7 天(25、24、23、22、18)的四舍五入平均值为 2。

我还希望能够扩展它并为平均超过 30 天添加另一列。

任何帮助将非常感激。

4

1 回答 1

0

我不是 100% 肯定我理解你的问题,但我相信你正在尝试SUM过去 7 天的 Orders 列,然后除以COUNT得到AVG. 如果是这样,那么这应该使用您提供的 Fiddle 工作:

declare @DateFrom datetime
    set @DateFrom = '2012-12-01'

    declare @DateTo datetime
    set @DateTo = '2013-03-13'

    ;with orders as (
        select 
        cast(o.ReceivedDate as date) as OrderDate, 
        count(oi.Id) as Orders,
        coalesce(sum(oi.Price), 0) as Price
        from OrderItem oi 
        join [Order] o on oi.OrderId = o.Id
        where cast(o.ReceivedDate as date) >= @DateFrom 
        and cast(o.ReceivedDate as date) <= @DateTo
        group by cast(o.ReceivedDate as date)
    ),
    totals as (
      select c1.OrderDate,
      c1.Price,
      c1.Orders,
      c1.Orders - c2.Orders as DIFF7DAYS, 
      ROW_NUMBER() OVER (ORDER BY c1.OrderDate DESC) rn
      from orders c1
      left join orders c2 on dateadd(day, -7, c1.OrderDate) = c2.OrderDate

  ) 
Select t.OrderDate, t.Price, t.Orders, t.Diff7Days,
  SUM(t2.Orders)/COUNT(t2.Orders) avg
FROM totals t
LEFT JOIN totals t2 on t.rn + 7 > t2.rn and t2.rn >= t.rn
GROUP BY t.OrderDate, t.Price, t.Orders, t.Diff7Days
order by t.OrderDate desc

SQL 小提琴演示

如果您想为那些没有 7 天数据的记录设置 NULL,那么一个简单CASE的检查语句COUNT就可以确定这一点。

于 2013-03-18T23:20:59.620 回答