我正在使用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日不为空。
非常感谢任何帮助。
答案 0 :(得分:2)
不确定为什么在left outer join
表和[Orders]
之间使用the subquery
,因为没有订单商品(通常)没有订单:
要获得结果,您可以使用CTE
;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) |
答案 1 :(得分: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