title: SQL query to retrieve WooCommerce orders grouped by state
body: <p>what I'm trying to retrieve from the Wordpress/WooCommerce database is a list of orders grouped by the states in the US with the Order totals and Order taxes summed for each state.</p>
  
  <p>So the closest I've gotten is this query I've written:</p>
  
  <pre><code>SELECT meta_key, meta_value 
  FROM wp_posts 
  LEFT OUTER JOIN wp_postmeta ON wp_posts.ID=wp_postmeta.post_id 
  WHERE post_type = "shop_order" 
  AND (
  meta_key = "_billing_state" 
  OR meta_key = "_order_tax" 
  OR meta_key = "_order_total"
  ) 
  ORDER BY ID ASC
  </code></pre>
  
  <p>And it returns:</p>
  
  <pre><code>meta_key         meta_value
  _billing_state   NJ
  _order_tax       0.21
  _order_total     3.21
  _billing_state   NJ
  _order_tax       1.75
  _order_total     26.70
  _billing_state   IA
  _order_tax       6.79
  _order_total     103.54
  _billing_state   PA
  _order_tax       1.82
  _order_total     27.72
  </code></pre>
  
  <p>...and so on.</p>
  
  <p>What I really need to return is:</p>
  
  <pre><code>state  order_tax_sum  order_total_sum
  NJ     1.96           29.91
  IA     6.79           103.54
  PA     1.82           27.72
  </code></pre>
  
  <p>All help is massively appreciated!</p>