作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个查询来向我展示付款之间的平均时间差异
SELECT TIMESTAMPDIFF(SECOND, MIN(x.ts), MAX(x.ts) )/(COUNT(DISTINCT(x.ts)) -1)
as average_minutes_between_paypal_orders
from (
select p.created_at as ts
from payment_tokens p
where status='processed' and payment_method = 'paypal'
order by created_at desc limit 100
) x
问题是,我希望它单独呈现其他方法(例如“braintree”、“其他”等)之间的平均时间差异,并且我不想编写多个查询。
有没有办法在 1 个查询中单独呈现所有 payment_methods?
最佳答案
使用CASE
进行所需计算的一种方法
SELECT
TIMESTAMPDIFF(SECOND
,MIN(CASE WHEN x.payment_method = 'paypal' THEN x.ts END)
,MAX(CASE WHEN x.payment_method = 'paypal' THEN x.ts END) )
/(COUNT(DISTINCT(CASE WHEN x.payment_method = 'paypal' THEN x.ts END)) -1) AS average_minutes_between_paypal_orders ,
TIMESTAMPDIFF(SECOND
,MIN(CASE WHEN x.payment_method = 'braintree' THEN x.ts END)
,MAX(CASE WHEN x.payment_method = 'braintree' THEN x.ts END) )
/(COUNT(DISTINCT(CASE WHEN x.payment_method = 'braintree' THEN x.ts END)) -1) AS average_minutes_between_braintree_orders
FROM (
SELECT p.created_at AS ts
FROM payment_tokens p
WHERE `status`='processed'
ORDER BY created_at DESC LIMIT 100
) `x`
关于mysql - TIMESTAMPDIFF 用于不止一种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25707520/
我是一名优秀的程序员,十分优秀!