我有一个查询,我可以运行以获取特定时间段内的平均订单价值,但我希望能够在一段时间内获得该时间段内的每一天以及第二列中的值。
这就是我想要的数据:
Day AOV
1/1/2015 342.83
1/2/2015 312.39
以下是我目前只能从特定时间段检索一个平均值的代码:
SELECT (sum(oi.price * oi.qty) / count(distinct o.orderid)) AS AOV
FROM dbo.Orders o
INNER JOIN dbo.OrdersItems oi ON oi.orderID = o.orderID
WHERE o.orderDate between '5/20/2015' AND '8/20/2015'
非常感谢任何帮助。谢谢!
答案 0 :(得分:1)
如果o.orderDate
碰巧是DATETIME
,只有小时/分钟/秒,而不仅仅是日期:
SELECT convert(date, o.orderDate) as orderDate
(sum(oi.price * oi.qty) / count(distinct o.orderid)) AS AOV
FROM dbo.Orders o
INNER JOIN dbo.OrdersItems oi ON oi.orderID = o.orderID
group by convert(date, o.orderDate)
WHERE o.orderDate between '5/20/2015' AND '8/20/2015'
答案 1 :(得分:0)
SELECT o.orderDate as "Day", (sum(oi.price * oi.qty) / count(distinct o.orderid)) AS AOV
FROM dbo.Orders o
INNER JOIN dbo.OrdersItems oi ON oi.orderID = o.orderID
WHERE o.orderDate between '5/20/2015' AND '8/20/2015'
GROUP BY o.OrderDate /* or cast(o.OrderDate as date) if it's a datetime */
ORDER BY "Day"
如果orderDate
是日期时间,那么您需要注意范围的结束。您的查询将包括最后一天的午夜,但之后不会包含任何其他内容。我怀疑情况并非如此。