返回介绍

solution / 2200-2299 / 2252.Dynamic Pivoting of a Table / README_EN

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

2252. Dynamic Pivoting of a Table

中文文档

Description

Table: Products

+-------------+---------+
| Column Name | Type  |
+-------------+---------+
| product_id  | int   |
| store     | varchar |
| price     | int   |
+-------------+---------+
(product_id, store) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates the price of product_id in store.
There will be at most 30 different stores in the table.
price is the price of the product at this store.

 

Important note: This problem targets those who have a good experience with SQL. If you are a beginner, we recommend that you skip it for now.

Implement the procedure PivotProducts to reorganize the Products table so that each row has the id of one product and its price in each store. The price should be null if the product is not sold in a store. The columns of the table should contain each store and they should be sorted in lexicographical order.

The procedure should return the table after reorganizing it.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Products table:
+------------+----------+-------+
| product_id | store  | price |
+------------+----------+-------+
| 1      | Shop   | 110   |
| 1      | LC_Store | 100   |
| 2      | Nozama   | 200   |
| 2      | Souq   | 190   |
| 3      | Shop   | 1000  |
| 3      | Souq   | 1900  |
+------------+----------+-------+
Output: 
+------------+----------+--------+------+------+
| product_id | LC_Store | Nozama | Shop | Souq |
+------------+----------+--------+------+------+
| 1      | 100    | null   | 110  | null |
| 2      | null   | 200  | null | 190  |
| 3      | null   | null   | 1000 | 1900 |
+------------+----------+--------+------+------+
Explanation: 
We have 4 stores: Shop, LC_Store, Nozama, and Souq. We first order them lexicographically to be: LC_Store, Nozama, Shop, and Souq.
Now, for product 1, the price in LC_Store is 100 and in Shop is 110. For the other two stores, the product is not sold so we set the price as null.
Similarly, product 2 has a price of 200 in Nozama and 190 in Souq. It is not sold in the other two stores.
For product 3, the price is 1000 in Shop and 1900 in Souq. It is not sold in the other two stores.

Solutions

Solution 1

CREATE PROCEDURE PivotProducts()
BEGIN
  # Write your MySQL query statement below.
  SET group_concat_max_len = 5000;
  SELECT GROUP_CONCAT(DISTINCT 'MAX(CASE WHEN store = \'',
         store,
         '\' THEN price ELSE NULL END) AS ',
         store
         ORDER BY store) INTO @sql
  FROM Products;
  SET @sql =  CONCAT('SELECT product_id, ',
          @sql,
          ' FROM Products GROUP BY product_id');
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
END

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

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

发布评论

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