Skip to content

Instantly share code, notes, and snippets.

@robindevitt
Last active April 22, 2020 11:01
Show Gist options
  • Save robindevitt/1ba9b498846d330a5fc573ea22a40830 to your computer and use it in GitHub Desktop.
Save robindevitt/1ba9b498846d330a5fc573ea22a40830 to your computer and use it in GitHub Desktop.
select
p.ID as order_id,
p.post_date,
max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
max( CASE WHEN pm.meta_key = '_customer_user' and p.ID = pm.post_id THEN pm.meta_value END ) as user_id,
us.user_registered,
if( us.user_registered < '2019-01-01', 'returning', 'new' ) as user_type
from
wp_posts p
join wp_postmeta pm on p.ID = pm.post_id
left join wp_users us on pm.meta_value = us.ID
where
post_type = 'shop_order' and
post_date BETWEEN '2019-01-01' AND '2020-04-15' and
pm.meta_key in ('_order_total', '_customer_user')
group by
p.ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment