我需要确定履行团队处理订单所需的平均时间。
我打算简单计算一下"秒"在" PaidDateUtc
"之间和" ShippedDateUtc
"每个订单的价值,然后得到平均值。
我的简化查询(减去日期范围过滤器)如下所示:
SELECT
dbo.Shipment.ShippedDateUtc - dbo.[Order].PaidDateUtc AS TimeToFulfill
FROM
dbo.[Order] INNER JOIN
dbo.Shipment ON dbo.[Order].Id = dbo.Shipment.OrderId
返回的值如下所示:
1900-01-01 00:25:27.263
这意味着: 1天,1小时,25分钟和27秒
我的问题是:如何操作它以使返回的值如下所示:
91527 --(total of seconds)
答案 0 :(得分:4)
从语法看起来您使用的是Microsoft SQL Server或Sybase,如果是这样,那么您可以使用DATEDIFF
函数来获取订单和发货之间的秒数,如下所示:
SELECT
DATEDIFF(second, o.PaidDateUtc, s.ShippedDateUtc) AS TimeToFulfill
FROM
dbo.[Order] o
INNER JOIN
dbo.Shipment s ON o.Id = s.OrderId
如果您希望每个订单的平均秒数包含DATEDIFF
AVG()
group by order.id
中的SELECT
o.id,
AVG(DATEDIFF(second, o.PaidDateUtc, s.ShippedDateUtc)) AS TimeToFulfill
FROM
dbo.[Order] o
INNER JOIN
dbo.Shipment s ON o.Id = s.OrderId
GROUP BY
o.id
:
{{1}}
答案 1 :(得分:1)
以秒为单位使用datediff()
。如果使用表别名,您的查询也会更简单:
SELECT DATEDIFF(SECOND, o.PaidDateUtc, s.ShippedDateUtc) AS TimeToFulfill
FROM dbo.[Order] o INNER JOIN
dbo.Shipment s
ON o.Id = s.OrderId;