使用SQL创建WooCommerce Analytical报告

时间:2022-12-04 15:51:33

I just recently moved to WooCommerce platform and need to build SQL in MySQL to run my own analytical reports. For example, I need report that shows total number of sales per sku, per country in the last 30 days. I explored tables like 'wp_posts', 'wp_postmeta' and found that most fields from products and orders are record-based, not column based which makes the SQL a little more challenging.

我刚刚搬到WooCommerce平台,需要在MySQL中构建SQL来运行我自己的分析报告。例如,我需要报告,显示过去30天内每个国家/地区的每个sku的总销售数量。我探索了像'wp_posts','wp_postmeta'这样的表,发现产品和订单中的大多数字段都是基于记录的,而不是基于列的,这使得SQL更具挑战性。

1 个解决方案

#1


3  

Finally built the SQL by myself. Hope it will be useful for other WooCommerce users who don't want to use analytical out-of-the box solutions or need custom reporting.

最后我自己构建了SQL。希望它对其他不想使用分析开箱即用解决方案或需要自定义报告的WooCommerce用户有用。

SELECT 
    sku, count(1)
FROM 
    wp_woocommerce_order_items oi, -- orders table
    wp_posts p, -- woocommerce use posts to keep metadata on the order
    wp_postmeta pm, -- we use this table to filter the completed orders
    wp_postmeta pm1, -- we use this table again for getting the shipping country info
    wp_woocommerce_order_itemmeta oim, -- use this table to get product ids from orders
    (SELECT p.id as product_id, meta_value as sku FROM wp_posts p, wp_postmeta pm  where 
        post_type='product' and post_status='publish' 
        and meta_key='_sku' 
        and p.id=pm.post_id) as sku_table -- building temp sku table from published products that defines relationship between product id and sku
WHERE 
    oim.meta_value=sku_table.product_id AND
    oi.order_id=p.ID AND 
    p.id=pm.post_id AND 
    pm.meta_key='_paid_date' AND  -- here we make sure that the order was completed 
    p.id=pm1.post_id AND 
    order_item_type='line_item' AND -- get only line items but not tax or shipping 
    oi.order_item_id=oim.order_item_id AND
    oim.meta_key='_product_id'  AND
    post_date>'2014-01-01' AND -- limits the report date
    pm1.meta_key='_shipping_country' AND -- get shipping country 
    pm1.meta_value='US' -- limits the country
GROUP BY sku
ORDER BY sku;

#1


3  

Finally built the SQL by myself. Hope it will be useful for other WooCommerce users who don't want to use analytical out-of-the box solutions or need custom reporting.

最后我自己构建了SQL。希望它对其他不想使用分析开箱即用解决方案或需要自定义报告的WooCommerce用户有用。

SELECT 
    sku, count(1)
FROM 
    wp_woocommerce_order_items oi, -- orders table
    wp_posts p, -- woocommerce use posts to keep metadata on the order
    wp_postmeta pm, -- we use this table to filter the completed orders
    wp_postmeta pm1, -- we use this table again for getting the shipping country info
    wp_woocommerce_order_itemmeta oim, -- use this table to get product ids from orders
    (SELECT p.id as product_id, meta_value as sku FROM wp_posts p, wp_postmeta pm  where 
        post_type='product' and post_status='publish' 
        and meta_key='_sku' 
        and p.id=pm.post_id) as sku_table -- building temp sku table from published products that defines relationship between product id and sku
WHERE 
    oim.meta_value=sku_table.product_id AND
    oi.order_id=p.ID AND 
    p.id=pm.post_id AND 
    pm.meta_key='_paid_date' AND  -- here we make sure that the order was completed 
    p.id=pm1.post_id AND 
    order_item_type='line_item' AND -- get only line items but not tax or shipping 
    oi.order_item_id=oim.order_item_id AND
    oim.meta_key='_product_id'  AND
    post_date>'2014-01-01' AND -- limits the report date
    pm1.meta_key='_shipping_country' AND -- get shipping country 
    pm1.meta_value='US' -- limits the country
GROUP BY sku
ORDER BY sku;