我如何知道客户每次事务之间的平均时间(以秒为单位)?
Time Customer ID Transaction
11/08/2020 00:00:01 1 111
11/08/2020 00:02:00 2 0
11/08/2020 00:02:07 1 0
11/08/2020 00:03:09 3 412
11/08/2020 00:04:00 1 0在预期的表之前,我需要显示所需的步骤:对于客户ID 1有3个事务,差异事务。
预期表:
Customer ID Average time between each transactions for customer
1 (126+113)/3
2
3 发布于 2017-01-02 13:54:40
平均时间是总时间除以交易数的1。所以:
select customerId,
(case when count(*) > 1
then datediff(second, min(time), max(time)) / (count(*) - 1)
end) as avg_time
from t
group by customerId;注意: Server执行整数除法。如果结果需要一个非整数,则可能需要在表达式中进行转换或count(*) - 1.0。
这确实假定时间只是在增加(对于这类问题,这似乎是一个合理的假设)。
https://stackoverflow.com/questions/41428041
复制相似问题