MySQL 查询“按国家和州/省列出的销售报告”

发布于 2024-09-06 11:38:10 字数 184 浏览 2 评论 0原文

客户需要一份按国家/地区/省份划分的销售报告。有一个订单表存储客户的国家/地区和州/省,还有一个订单产品表存储订单与产品的关联以及产品的价格。

客户希望获得每个国家/地区的以下统计数据州/省:总销售额、销售额百分比、平均订单价值。

我希望我已经提供了足够的信息。如果您还需要任何其他信息,请告诉我。

谢谢!

A client needs a sales report by country state/province. There is an orders table that stores the customer's country and state/province, and there is an orders_products table that stores the order to product association and the product's price.

The client wants the following statistics per country & state/province: total sales, % sales, avg order value.

I hope I have provided enough information. If you need anything else, please let me know.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-09-13 11:38:10

== 假设表 ==

订单 (id、country_id、state_id)

订单产品 (id、order_id、product_id、价格)

== 查询 ==

总销售额:

SELECT country_id, state_id, SUM(price)
FROM orders_products op, orders o 
WHERE op.order_id = o.id 
GROUP BY country_id, state_id

平均销售额:

SELECT country_id, state_id, AVG(price) * COUNT(order_id) 
FROM orders_products op, orders o 
WHERE op.order_id = o.id 
GROUP BY country_id, state_id, order_id

每个州/国家/地区总销售额的百分比:

SELECT country_id, state_id, SUM(price) * 100 /
    (SELECT SUM(price) FROM orders_products op2) 
FROM orders_products op, orders o 
WHERE op.order_id = o.id 
GROUP BY country_id, state_id

== Assumed Tables ==

orders (id, country_id, state_id)

orders_products (id, order_id, product_id, price)

== Queries ==

Total sales:

SELECT country_id, state_id, SUM(price)
FROM orders_products op, orders o 
WHERE op.order_id = o.id 
GROUP BY country_id, state_id

Average Sale Amount:

SELECT country_id, state_id, AVG(price) * COUNT(order_id) 
FROM orders_products op, orders o 
WHERE op.order_id = o.id 
GROUP BY country_id, state_id, order_id

The % of total sales per state/country:

SELECT country_id, state_id, SUM(price) * 100 /
    (SELECT SUM(price) FROM orders_products op2) 
FROM orders_products op, orders o 
WHERE op.order_id = o.id 
GROUP BY country_id, state_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文