3

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

我正在尝试编写一个查询来获取日期范围内的所有订单,按日期对它们进行分组,然后对所有订单商品的价格求和以获得该日期所有订单的总数。

我有这个工作。现在我想添加另一列来选择当天总价与 7 天前之间的差额。如果 7 天前没有订单,则该列应为空。

所以目前我有以下查询:

select cast(o.ReceivedDate as date) as OrderDate, 
coalesce(count(orderItems.orderId), 0) as Orders,
coalesce(sum(orderItems.Price), 0) as Price
from [Order] o

left outer join (
        select o.Id as orderId, sum(ot.Price) as Price
        from OrderItem ot
        join [Order] o on ot.OrderId = o.Id
        where o.ReceivedDate >= @DateFrom and o.ReceivedDate <= @DateTo
        group by o.Id
) as orderItems on o.Id = orderItems.orderId

where o.ReceivedDate >= @DateFrom and o.ReceivedDate <= @DateTo
group by cast(o.ReceivedDate as date)
order by cast(o.ReceivedDate as date) desc

那么如何将我的其他列添加到此查询中?我需要做类似的事情:

//pseudo 
if o.RecievedDate - 7 exists then orderItems.Price - Price from 7 days ago else null

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

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

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

如您所见,25 日有 7 天前的订单,因此显示了差异。24 号显示的不是那么空。

任何帮助将非常感激。

4

2 回答 2

2

不知道为什么要使用left outer joinbetween[Orders]表,the subquery因为没有订单项就不能有订单(通常):

要获得结果,您可以使用以下简化版本进行操作CTE

SQL-FIDDLE-DEMO

;with cte as (
  select convert(date,o.ReceivedDate) orderDate,
       count(distinct o.Id) as Orders,
       coalesce(sum(ot.Price),0) as Price
  from OrderItem ot
        join [Order] o on ot.OrderId = o.Id
  where o.ReceivedDate >= @DateFrom and o.ReceivedDate <= @DateTo
  group by convert(date,o.ReceivedDate)
)
select c1.orderDate, c1.Orders, c1.Price, c1.Price-c2.Price DIFF7DAYS
from cte c1 left join cte c2 on dateadd(day,-7,c1.orderdate) = c2.orderdate
order by c1.orderdate desc


|  ORDERDATE | ORDERS | PRICE | DIFF7DAYS |
-------------------------------------------
| 2013-01-25 |      3 |    38 |        28 |
| 2013-01-24 |      1 |    12 |    (null) |
| 2013-01-23 |      1 |    10 |    (null) |
| 2013-01-22 |      1 |    33 |    (null) |
| 2013-01-18 |      1 |    10 |    (null) |
| 2013-01-10 |      1 |     3 |       -43 |
| 2013-01-08 |      2 |    11 |    (null) |
| 2013-01-04 |      1 |     1 |    (null) |
| 2013-01-03 |      3 |    46 |    (null) |
于 2013-03-13T23:13:51.870 回答
1

使用临时表并将其加入日期差异。

DECLARE @DateFrom datetime
SET @DateFrom = '2012-12-02'

DECLARE @DateTo datetime
SET @DateTo = '2013-03-13'

CREATE TABLE #temp ( orderdate date, orders int, price money)
INSERT INTO #temp
SELECT cast(o.ReceivedDate AS date) AS OrderDate,
coalesce(count(orderItems.orderId), 0) AS Orders,
coalesce(sum(orderItems.Price), 0) AS Price
FROM [Order] o

LEFT OUTER JOIN (
SELECT o.Id AS orderId, sum(ot.Price) AS Price
FROM OrderItem ot
JOIN [Order] o ON ot.OrderId = o.Id
WHERE o.ReceivedDate >= @DateFrom AND o.ReceivedDate <= @DateTo
GROUP BY o.Id
) AS orderItems ON o.Id = orderItems.orderId

WHERE o.ReceivedDate >= @DateFrom AND o.ReceivedDate <= @DateTo
GROUP BY cast(o.ReceivedDate AS date)

SELECT t1.orderdate, t1.orders, t1.price, 
t1.price - t2.price AS diff7days
FROM #temp t1 LEFT JOIN #temp t2
ON datediff(DAY, t2.orderdate, t1.orderdate) = 7
ORDER BY t1.orderdate DESC

http://sqlfiddle.com/#!6/8b837/34

于 2013-03-13T23:07:39.897 回答