在聚合查询中使用counts和sum

y53ybaqx  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(317)

更新:看起来像是强制报警,我误解了我的结果集,实际上查询返回它应该返回的结果。哎呀。非常感谢大家的帮助!
我的问题怎么了?产生了错误的结果。

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
jyztefdp

jyztefdp1#

你能试试这个查询吗?

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

相关问题