{ "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
\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
\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\n\n\n

Return the result table ordered by imbalance ratio in descending order, then by store name in ascending order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

stores 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\n\n\n

The Results table is ordered by imbalance ratio in descending order, then by store name in ascending order

\n
\n", "translatedTitle": "查找库存不平衡的店铺", "translatedContent": "

表:stores

\n\n
\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
\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\n\n\n

返回结果表以不平衡比率降序排列,然后按商店名称升序排列。

\n\n

结果格式如下所示。

\n\n

 

\n\n

示例:

\n\n
\n

输入:

\n\n

stores 表:

\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\n

结果表按不平衡比降序排序,然后以商店名升序排序。

\n
\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 1, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef find_inventory_imbalance(stores: pd.DataFrame, inventory: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"238\", \"totalSubmission\": \"410\", \"totalAcceptedRaw\": 238, \"totalSubmissionRaw\": 410, \"acRate\": \"58.0%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"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]]}}", "metaData": "{\"mysql\":[\"CREATE TABLE if not exists stores (\\n store_id INT,\\n store_name VARCHAR(255),\\n location VARCHAR(255)\\n)\",\"CREATE TABLE if not exists inventory (\\n inventory_id INT,\\n store_id INT,\\n product_name VARCHAR(255),\\n quantity INT,\\n price DECIMAL(10, 2)\\n)\"],\"mssql\":[\"CREATE TABLE stores (\\n store_id INT,\\n store_name VARCHAR(255),\\n location VARCHAR(255)\\n)\",\"CREATE TABLE inventory (\\n inventory_id INT,\\n store_id INT,\\n product_name VARCHAR(255),\\n quantity INT,\\n price DECIMAL(10, 2)\\n)\"],\"oraclesql\":[\"CREATE TABLE stores (\\n store_id NUMBER,\\n store_name VARCHAR2(255),\\n location VARCHAR2(255)\\n)\",\"CREATE TABLE inventory (\\n inventory_id NUMBER,\\n store_id NUMBER,\\n product_name VARCHAR2(255),\\n quantity NUMBER,\\n price NUMBER(10, 2)\\n)\"],\"database\":true,\"name\":\"find_inventory_imbalance\",\"postgresql\":[\"CREATE TABLE IF NOT EXISTS stores (\\n store_id INTEGER,\\n store_name VARCHAR(255),\\n location VARCHAR(255)\\n);\\n\",\"CREATE TABLE IF NOT EXISTS inventory (\\n inventory_id INTEGER,\\n store_id INTEGER,\\n product_name VARCHAR(255),\\n quantity INTEGER,\\n price DECIMAL(10, 2)\\n);\\n\"],\"pythondata\":[\"stores = pd.DataFrame([], columns=['store_id', 'store_name', 'location']).astype({'store_id': 'int64', 'store_name': 'string', 'location': 'string'})\\n\",\"inventory = pd.DataFrame([], columns=['inventory_id', 'store_id', 'product_name', 'quantity', 'price']).astype({'inventory_id': 'int64', 'store_id': 'int64', 'product_name': 'string', 'quantity': 'int64', 'price': 'float64'})\\n\"],\"database_schema\":{\"stores\":{\"store_id\":\"INT\",\"store_name\":\"VARCHAR(255)\",\"location\":\"VARCHAR(255)\"},\"inventory\":{\"inventory_id\":\"INT\",\"store_id\":\"INT\",\"product_name\":\"VARCHAR(255)\",\"quantity\":\"INT\",\"price\":\"DECIMAL(10, 2)\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "CREATE TABLE if not exists stores (\n store_id INT,\n store_name VARCHAR(255),\n location VARCHAR(255)\n)", "CREATE TABLE if not exists inventory (\n inventory_id INT,\n store_id INT,\n product_name VARCHAR(255),\n quantity INT,\n price DECIMAL(10, 2)\n)", "Truncate table stores", "insert into stores (store_id, store_name, location) values ('1', 'Downtown Tech', 'New York')", "insert into stores (store_id, store_name, location) values ('2', 'Suburb Mall', 'Chicago')", "insert into stores (store_id, store_name, location) values ('3', 'City Center', 'Los Angeles')", "insert into stores (store_id, store_name, location) values ('4', 'Corner Shop', 'Miami')", "insert into stores (store_id, store_name, location) values ('5', 'Plaza Store', 'Seattle')", "Truncate table inventory", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('1', '1', 'Laptop', '5', '999.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('2', '1', 'Mouse', '50', '19.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('3', '1', 'Keyboard', '25', '79.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('4', '1', 'Monitor', '15', '299.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('5', '2', 'Phone', '3', '699.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('6', '2', 'Charger', '100', '25.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('7', '2', 'Case', '75', '15.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('8', '2', 'Headphones', '20', '149.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('9', '3', 'Tablet', '2', '499.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('10', '3', 'Stylus', '80', '29.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('11', '3', 'Cover', '60', '39.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('12', '4', 'Watch', '10', '299.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('13', '4', 'Band', '25', '49.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('14', '5', 'Camera', '8', '599.99')", "insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('15', '5', 'Lens', '12', '199.99')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

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" } } }