我正在尝试对以下查询返回的列求和,
select
max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END ) as order_total
from
wp_posts p
join wp_postmeta pm on p.ID = pm.post_id
join wp_woocommerce_order_items oi on p.ID = oi.order_id
where
post_type = 'shop_order' and
post_date BETWEEN '2016-01-01' AND '2018-07-08' and
post_status = 'wc-completed'
group by
p.ID
我试过以下方法,
select
SUM(max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END )) as order_total
from
wp_posts p
join wp_postmeta pm on p.ID = pm.post_id
join wp_woocommerce_order_items oi on p.ID = oi.order_id
where
post_type = 'shop_order' and
post_date BETWEEN '2016-01-01' AND '2018-07-08' and
post_status = 'wc-completed'
group by
p.ID
我的错误越来越小,
#1111 - Invalid use of group function