返回介绍

solution / 2900-2999 / 2985.Calculate Compressed Mean / README_EN

发布于 2024-06-17 01:02:58 字数 2452 浏览 0 评论 0 收藏 0

2985. Calculate Compressed Mean

中文文档

Description

Table: Orders

+-------------------+------+
| Column Name     | Type |
+-------------------+------+
| order_id      | int  |
| item_count    | int  |
| order_occurrences | int  |
+-------------------+------+
order_id is column of unique values for this table.
This table contains order_id, item_count, and order_occurrences.

Write a solution to calculate the average number of items per order, rounded to 2 decimal places.

Return _the result table__ in any order__._

The result format is in the following example.

 

Example 1:

Input: 
Orders table:
+----------+------------+-------------------+
| order_id | item_count | order_occurrences | 
+----------+------------+-------------------+
| 10     | 1      | 500         | 
| 11     | 2      | 1000        |   
| 12     | 3      | 800         |  
| 13     | 4      | 1000        | 
+----------+------------+-------------------+
Output
+-------------------------+
| average_items_per_order | 
+-------------------------+
| 2.70          |
+-------------------------+
Explanation
The calculation is as follows:
 - Total items: (1 * 500) + (2 * 1000) + (3 * 800) + (4 * 1000) = 8900 
 - Total orders: 500 + 1000 + 800 + 1000 = 3300 
 - Therefore, the average items per order is 8900 / 3300 = 2.70

Solutions

Solution 1: Summation

We use the SUM function to calculate the total quantity of products and the total number of orders, then divide the total quantity by the total number of orders to get the average. Finally, we use the ROUND function to round the result to two decimal places.

# Write your MySQL query statement below
SELECT
  ROUND(
    SUM(item_count * order_occurrences) / SUM(order_occurrences),
    2
  ) AS average_items_per_order
FROM Orders;

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文