返回介绍

solution / 1500-1599 / 1571.Warehouse Manager / README_EN

发布于 2024-06-17 01:03:17 字数 3915 浏览 0 评论 0 收藏 0

1571. Warehouse Manager

中文文档

Description

Table: Warehouse

+--------------+---------+
| Column Name  | Type  |
+--------------+---------+
| name     | varchar |
| product_id   | int   |
| units    | int   |
+--------------+---------+
(name, product_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the information of the products in each warehouse.

 

Table: Products

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| product_id  | int   |
| product_name  | varchar |
| Width     | int   |
| Length    | int   |
| Height    | int   |
+---------------+---------+
product_id is the primary key (column with unique values) for this table.
Each row of this table contains information about the product dimensions (Width, Lenght, and Height) in feets of each product.

 

Write a solution to report the number of cubic feet of volume the inventory occupies in each warehouse.

Return the result table in any order.

The query result format is in the following example.

 

Example 1:

Input: 
Warehouse table:
+------------+--------------+-------------+
| name     | product_id   | units     |
+------------+--------------+-------------+
| LCHouse1   | 1      | 1       |
| LCHouse1   | 2      | 10      |
| LCHouse1   | 3      | 5       |
| LCHouse2   | 1      | 2       |
| LCHouse2   | 2      | 2       |
| LCHouse3   | 4      | 1       |
+------------+--------------+-------------+
Products table:
+------------+--------------+------------+----------+-----------+
| product_id | product_name | Width    | Length   | Height  |
+------------+--------------+------------+----------+-----------+
| 1      | LC-TV    | 5      | 50     | 40    |
| 2      | LC-KeyChain  | 5      | 5    | 5     |
| 3      | LC-Phone   | 2      | 10     | 10    |
| 4      | LC-T-Shirt   | 4      | 10     | 20    |
+------------+--------------+------------+----------+-----------+
Output: 
+----------------+------------+
| warehouse_name | volume   | 
+----------------+------------+
| LCHouse1     | 12250    | 
| LCHouse2     | 20250    |
| LCHouse3     | 800    |
+----------------+------------+
Explanation: 
Volume of product_id = 1 (LC-TV), 5x50x40 = 10000
Volume of product_id = 2 (LC-KeyChain), 5x5x5 = 125 
Volume of product_id = 3 (LC-Phone), 2x10x10 = 200
Volume of product_id = 4 (LC-T-Shirt), 4x10x20 = 800
LCHouse1: 1 unit of LC-TV + 10 units of LC-KeyChain + 5 units of LC-Phone.
      Total volume: 1*10000 + 10*125  + 5*200 = 12250 cubic feet
LCHouse2: 2 units of LC-TV + 2 units of LC-KeyChain.
      Total volume: 2*10000 + 2*125 = 20250 cubic feet
LCHouse3: 1 unit of LC-T-Shirt.
      Total volume: 1*800 = 800 cubic feet.

Solutions

Solution 1: Inner Join + Group By + Sum Function

We can use an inner join to join the Warehouse table and the Products table on the condition of product_id, and then group by warehouse name to calculate the inventory of each warehouse using the SUM function.

# Write your MySQL query statement below
SELECT
  name AS warehouse_name,
  SUM(width * length * height * units) AS volume
FROM
  Warehouse
  JOIN Products USING (product_id)
GROUP BY 1;

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

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

发布评论

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