在汇总查询中使用计数和总和

时间:2018-08-21 16:31:37

标签: mysql

更新:似乎是强制警报,我误解了结果集,实际上查询返回了应返回的内容。哎呀。非常感谢大家的帮助!

我的查询出了什么问题?结果不正确。

SELECT
    utm_source as source,
    utm_medium as medium,
    utm_campaign as campaign,
    utm_content as content,
    COUNT(order_id) as orders,
    COUNT(DISTINCT _customer_user) as customers,
    SUM(_order_total) as revenue,
    SUM(_refund_amount) as refunded,
    SUM(_order_total) - SUM(_refund_amount) as net,
    (SUM(_order_total) - SUM(_refund_amount))/COUNT(DISTINCT _customer_user) as 'average ticket'
FROM wp_realtime_utm_tracking_utms
GROUP BY source, medium, campaign, content

1 个答案:

答案 0 :(得分:1)

能否请您尝试此查询?

SELECT utm_source as source, utm_medium as medium, utm_campaign as campaign, utm_content as content, COUNT(order_id) as orders, COUNT(DISTINCT _customer_user) as customers, SUM(_order_total) as revenue, SUM(_refund_amount) as refunded, (SUM(_order_total) - SUM(_refund_amount)) as net, ((SUM(_order_total) - SUM(_refund_amount))/COUNT(DISTINCT _customer_user)) as 'average ticket' FROM wp_realtime_utm_tracking_utms GROUP BY 1,2,3,4