我正在编写一条SQL命令来尝试获取最近30次的平均交易持续时间
SELECT SUM (end_time - start_time) as sum, 30 as count
FROM orders
WHERE customer_id = ".$customer_id." AND status = 'end'
如何在开始时编辑总和部分,所以我只能得到前30个平均值,因为目前它取每一行的总和
欢呼
杰克
答案 0 :(得分:0)
您可以从仅检索30个最小的订单(按start_time
)的子查询中进行选择。
语法取决于您的DBMS。
例如
SELECT sum(end_time - start_time),
30 count
FROM (SELECT start_time,
end_time
FROM orders
ORDER BY start_time DESC
LIMIT 30) x;
或
SELECT sum(end_time - start_time),
30 count
FROM (SELECT TOP 30
start_time,
end_time
FROM orders
ORDER BY start_time DESC) x;
或者其他人。
也许这可以帮助您指出正确的方向。