{ "data": { "question": { "questionId": "3971", "questionFrontendId": "3626", "categoryTitle": "Database", "boundTopicId": 3732905, "title": "Find Stores with Inventory Imbalance", "titleSlug": "find-stores-with-inventory-imbalance", "content": "
Table: stores
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| store_id | int |\n| store_name | varchar |\n| location | varchar |\n+-------------+---------+\nstore_id is the unique identifier for this table.\nEach row contains information about a store and its location.\n\n\n
Table: inventory
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| inventory_id| int |\n| store_id | int |\n| product_name| varchar |\n| quantity | int |\n| price | decimal |\n+-------------+---------+\ninventory_id is the unique identifier for this table.\nEach row represents the inventory of a specific product at a specific store.\n\n\n
Write a solution to find stores that have inventory imbalance - stores where the most expensive product has lower stock than the cheapest product.
\n\n3
different productsReturn the result table ordered by imbalance ratio in descending order, then by store name in ascending order.
\n\nThe result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\nstores table:
\n\n\n+----------+----------------+-------------+\n| store_id | store_name | location |\n+----------+----------------+-------------+\n| 1 | Downtown Tech | New York |\n| 2 | Suburb Mall | Chicago |\n| 3 | City Center | Los Angeles |\n| 4 | Corner Shop | Miami |\n| 5 | Plaza Store | Seattle |\n+----------+----------------+-------------+\n\n\n
inventory table:
\n\n\n+--------------+----------+--------------+----------+--------+\n| inventory_id | store_id | product_name | quantity | price |\n+--------------+----------+--------------+----------+--------+\n| 1 | 1 | Laptop | 5 | 999.99 |\n| 2 | 1 | Mouse | 50 | 19.99 |\n| 3 | 1 | Keyboard | 25 | 79.99 |\n| 4 | 1 | Monitor | 15 | 299.99 |\n| 5 | 2 | Phone | 3 | 699.99 |\n| 6 | 2 | Charger | 100 | 25.99 |\n| 7 | 2 | Case | 75 | 15.99 |\n| 8 | 2 | Headphones | 20 | 149.99 |\n| 9 | 3 | Tablet | 2 | 499.99 |\n| 10 | 3 | Stylus | 80 | 29.99 |\n| 11 | 3 | Cover | 60 | 39.99 |\n| 12 | 4 | Watch | 10 | 299.99 |\n| 13 | 4 | Band | 25 | 49.99 |\n| 14 | 5 | Camera | 8 | 599.99 |\n| 15 | 5 | Lens | 12 | 199.99 |\n+--------------+----------+--------------+----------+--------+\n\n\n
Output:
\n\n\n+----------+----------------+-------------+------------------+--------------------+------------------+\n| store_id | store_name | location | most_exp_product | cheapest_product | imbalance_ratio |\n+----------+----------------+-------------+------------------+--------------------+------------------+\n| 3 | City Center | Los Angeles | Tablet | Stylus | 40.00 |\n| 1 | Downtown Tech | New York | Laptop | Mouse | 10.00 |\n| 2 | Suburb Mall | Chicago | Phone | Case | 25.00 |\n+----------+----------------+-------------+------------------+--------------------+------------------+\n\n\n
Explanation:
\n\nThe Results table is ordered by imbalance ratio in descending order, then by store name in ascending order
\n表:stores
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| store_id | int |\n| store_name | varchar |\n| location | varchar |\n+-------------+---------+\nstore_id 是这张表的唯一主键。\n每一行包含有关商店及其位置的信息。\n\n\n
表:inventory
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| inventory_id| int |\n| store_id | int |\n| product_name| varchar |\n| quantity | int |\n| price | decimal |\n+-------------+---------+\ninventory_id 是这张表的唯一主键。\n每一行代表特定商店中某一特定产品的库存情况。\n\n\n
编写一个解决方案来查找库存不平衡的商店 - 即最贵商品的库存比最便宜商品少的商店。
\n\n3
个不同商品 的店铺返回结果表以不平衡比率降序排列,然后按商店名称升序排列。
\n\n结果格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\nstores 表:
\n\n\n+----------+----------------+-------------+\n| store_id | store_name | location |\n+----------+----------------+-------------+\n| 1 | Downtown Tech | New York |\n| 2 | Suburb Mall | Chicago |\n| 3 | City Center | Los Angeles |\n| 4 | Corner Shop | Miami |\n| 5 | Plaza Store | Seattle |\n+----------+----------------+-------------+\n\n\n
inventory 表:
\n\n\n+--------------+----------+--------------+----------+--------+\n| inventory_id | store_id | product_name | quantity | price |\n+--------------+----------+--------------+----------+--------+\n| 1 | 1 | Laptop | 5 | 999.99 |\n| 2 | 1 | Mouse | 50 | 19.99 |\n| 3 | 1 | Keyboard | 25 | 79.99 |\n| 4 | 1 | Monitor | 15 | 299.99 |\n| 5 | 2 | Phone | 3 | 699.99 |\n| 6 | 2 | Charger | 100 | 25.99 |\n| 7 | 2 | Case | 75 | 15.99 |\n| 8 | 2 | Headphones | 20 | 149.99 |\n| 9 | 3 | Tablet | 2 | 499.99 |\n| 10 | 3 | Stylus | 80 | 29.99 |\n| 11 | 3 | Cover | 60 | 39.99 |\n| 12 | 4 | Watch | 10 | 299.99 |\n| 13 | 4 | Band | 25 | 49.99 |\n| 14 | 5 | Camera | 8 | 599.99 |\n| 15 | 5 | Lens | 12 | 199.99 |\n+--------------+----------+--------------+----------+--------+\n\n\n
输出:
\n\n\n+----------+----------------+-------------+------------------+--------------------+------------------+\n| store_id | store_name | location | most_exp_product | cheapest_product | imbalance_ratio |\n+----------+----------------+-------------+------------------+--------------------+------------------+\n| 3 | City Center | Los Angeles | Tablet | Stylus | 40.00 |\n| 1 | Downtown Tech | New York | Laptop | Mouse | 10.00 |\n| 2 | Suburb Mall | Chicago | Phone | Case | 25.00 |\n+----------+----------------+-------------+------------------+--------------------+------------------+\n\n\n
解释:
\n\n结果表按不平衡比降序排序,然后以商店名升序排序。
\n\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"stores\":[\"store_id\",\"store_name\",\"location\"],\"inventory\":[\"inventory_id\",\"store_id\",\"product_name\",\"quantity\",\"price\"]},\"rows\":{\"stores\":[[1,\"Downtown Tech\",\"New York\"],[2,\"Suburb Mall\",\"Chicago\"],[3,\"City Center\",\"Los Angeles\"],[4,\"Corner Shop\",\"Miami\"],[5,\"Plaza Store\",\"Seattle\"]],\"inventory\":[[1,1,\"Laptop\",5,999.99],[2,1,\"Mouse\",50,19.99],[3,1,\"Keyboard\",25,79.99],[4,1,\"Monitor\",15,299.99],[5,2,\"Phone\",3,699.99],[6,2,\"Charger\",100,25.99],[7,2,\"Case\",75,15.99],[8,2,\"Headphones\",20,149.99],[9,3,\"Tablet\",2,499.99],[10,3,\"Stylus\",80,29.99],[11,3,\"Cover\",60,39.99],[12,4,\"Watch\",10,299.99],[13,4,\"Band\",25,49.99],[14,5,\"Camera\",8,599.99],[15,5,\"Lens\",12,199.99]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"