1

我正在尝试在 MySQL 中创建一个报告(来自 Wordpress/Woocommerce 安装中的表,但是这家商店的税收方式与 Woocommerce 做事的方式不同,所以我不能使用 Woocommerce 报告。)

该计算需要从总额中计算出 20% 的税率(英国增值税)——但前提是客户位于特定地点。因此,如果客户在“世界”中,税率为 0%。如果客户在英国,税率为 20%。但总收费保持不变(因此出口利润高于本地销售!)

我从:

SELECT TRIM(LEADING 'Order –' FROM post_title) AS Date,
m1.meta_value AS _order_number,
m2.meta_value AS _Location,
m3.meta_value AS _order_total
FROM wp_posts 
LEFT JOIN wp_postmeta AS m1 ON m1.post_id = wp_posts.ID AND m1.meta_key = '_order_number'
LEFT JOIN wp_postmeta AS m2 ON m2.post_id = wp_posts.ID AND m2.meta_key = '_shipping_method_title'
LEFT JOIN wp_postmeta AS m3 ON m3.post_id = wp_posts.ID AND m3.meta_key = '_order_total'
WHERE wp_posts.post_type = 'shop_order'

这给了我一张这样的桌子:

Date     |  Order no  | Location    |  Order Total

May 2013 |  123       |World         |1124.00

Jan 2013 |  124       |UK            |163.00

到目前为止,一切都很好。因此,我尝试添加一些 SUM 来计算增值税金额。

SELECT TRIM(LEADING 'Order –' FROM post_title) AS Date,
m1.meta_value AS _order_number,
m2.meta_value AS _Location,
m3.meta_value AS _order_total,
SUM(m3.meta_value*20/120) AS _VAT,
SUM(m3.meta_value*100/120) AS _NET
FROM wp_posts 
LEFT JOIN wp_postmeta AS m1 ON m1.post_id = wp_posts.ID AND m1.meta_key = '_order_number'
LEFT JOIN wp_postmeta AS m2 ON m2.post_id = wp_posts.ID AND m2.meta_key = '_shipping_method_title'
LEFT JOIN wp_postmeta AS m3 ON m3.post_id = wp_posts.ID AND m3.meta_key = '_order_total'
WHERE wp_posts.post_type = 'shop_order'

我认为这会给我:

Date       |     Order no | Location  |    Order Total | VAT  |   NET

May 2013  |             123|   World |    1124.00  |    187.33 |   1311.33

Jan 2013  |         124  |    UK     |      163.00   |    27.17 |  190.17

然后我可以继续尝试找出如何按位置更改费率。不幸的是,它似乎对所有内容求和,而不是逐行给我结果。

1)我把所有东西都加在一起了,我搞砸了什么?

2) 关于如何仅在 _shipping_method_title=UK 时最好地应用增值税计算的任何想法?

3) 显然,这仅在税率发生变化之前有效。处理这个问题的最佳选择是在下次修改税率时限制此报告的日期,并为适用新税率的未来订单制作新报告?还是有更聪明的方法?

我很欣赏向客户收取统一费率并支付可变税是一种有点奇怪的方法,但我无法改变这一点,我只是负责提供报告。:-(

编辑 感谢 Gordon,我已经解决了我的 SUM 问题,现在我的查询看起来像这样并产生了我期望的表格布局,如上所示:

SELECT TRIM(LEADING 'Order –' FROM post_title) AS Date,
m1.meta_value AS _order_number,
m2.meta_value AS _Location,
m3.meta_value AS _order_total,
SUM(m3.meta_value*20/120) AS _VAT,
SUM(m3.meta_value*100/120) AS _NET
FROM wp_posts 
LEFT JOIN wp_postmeta AS m1 ON m1.post_id = wp_posts.ID AND m1.meta_key = '_order_number'
LEFT JOIN wp_postmeta AS m2 ON m2.post_id = wp_posts.ID AND m2.meta_key = '_shipping_method_title'
LEFT JOIN wp_postmeta AS m3 ON m3.post_id = wp_posts.ID AND m3.meta_key = '_order_total'
WHERE wp_posts.post_type = 'shop_order'
GROUP BY wp_posts.ID

但我仍然不确定如何根据位置字段中的值更改乘数。这可能吗,还是我在这里吠错了树?

编辑 2

我已经制定了一个部分解决方案 - 而不是 SUM 行,我使用了一个 CASE 来给我一个增值税的计算值。

CASE 
        when m2.meta_value = 'World Delivery' then 0
        when m2.meta_value = 'UK Delivery' then  (m3.meta_value*20/120)
        when m2.meta_value = 'European Delivery' then  (m3.meta_value*20/120)
END AS _VAT    

不幸的是,我不能只为 _NET 值(价格 - 增值税)重复 CASE 语句,因为它与增值税的 CASE 相同,因此会产生错误。

我想做类似的事情

 SUM(_order_total-_VAT) AS _NET

但是 *_order_total* 和 *_VAT* 不被识别为可以计算的列。

最终编辑 再次感谢 Gordon,我的最终查询看起来像这样,我只是添加它以防其他人发现它有帮助。此查询与 Wordpress Exports and Reports wordpress 插件一起使用,因此可以快速集成到 Wordpress 管理员中并导出到 Excel,这很漂亮。

SELECT TRIM(LEADING 'Order –' FROM post_title) AS Date,
m1.meta_value AS _order_number,
m2.meta_value AS _Customer_Location,
m3.meta_value AS _order_total,

ROUND(SUM(case when m2.meta_value = 'World Delivery' then 0 else m3.meta_value*20/120 end),2) AS _VAT,
ROUND(SUM(case when m2.meta_value = 'World Delivery' then m3.meta_value else m3.meta_value*100/120 end),2) AS _NET
FROM wp_posts 
LEFT JOIN wp_postmeta AS m1 ON m1.post_id = wp_posts.ID AND m1.meta_key = '_order_number'
LEFT JOIN wp_postmeta AS m2 ON m2.post_id = wp_posts.ID AND m2.meta_key = '_shipping_method_title'
LEFT JOIN wp_postmeta AS m3 ON m3.post_id = wp_posts.ID AND m3.meta_key = '_order_total'
WHERE wp_posts.post_type = 'shop_order' AND wp_comments.comment_post_id = wp_posts.ID  AND wp_comments.comment_content="Order status changed from processing to completed."
GROUP BY wp_posts.ID
4

1 回答 1

4

你只得到一行的原因是因为该sum()函数使它成为一个聚合查询。但是,您没有group by. 我认为按 postid 分组可以解决这个问题,以及对以下方面的一些其他调整select

SELECT TRIM(LEADING 'Order –' FROM post_title) AS Date,
max(m1.meta_value) AS _order_number,
max(m2.meta_value) AS _Customer_Location,
max(m3.meta_value) AS _order_total,
SUM(m3.meta_value*20/120) AS _VAT,
SUM(m3.meta_value*100/120) AS _NET
FROM wp_posts 
LEFT JOIN wp_postmeta AS m1 ON m1.post_id = wp_posts.ID AND m1.meta_key = '_order_number'
LEFT JOIN wp_postmeta AS m2 ON m2.post_id = wp_posts.ID AND m2.meta_key = '_shipping_method_title'
LEFT JOIN wp_postmeta AS m3 ON m3.post_id = wp_posts.ID AND m3.meta_key = '_order_total'
WHERE wp_posts.post_type = 'shop_order'

这种类型的数据很难习惯。单个记录的所有数据都拆分为多行。

要根据位置更改乘数,请使用where子句。这是一个例子:

SUM(case when location = 'UK' then 0 else m3.meta_value*20/120 end) AS _VAT,
SUM(case when location = 'UK' then m3.meta_value else m3.meta_value*100/120 end) AS _NET
于 2013-05-22T00:38:59.530 回答