2252. Dynamic Pivoting of a Table(目前不会)(No)

SQL架构

Table: Products

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| product_id  | int     |
| store       | varchar |
| price       | int     |
+-------------+---------+
(product_id, store) is the primary key 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 query 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.

别人的代码:

CREATE PROCEDURE PivotProducts()
BEGIN
	# Write your MySQL query statement below.
	SET group_concat_max_len=100000;
SET @sql = NULL;
#SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(CASE WHEN store= ''',store,''' THEN price ELSE null END) AS ', store) ORDER BY store)  #case when 和 if 都可以
SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(store= ''',store,''', price, null)) 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
CREATE PROCEDURE PivotProducts()
BEGIN
	# Write your MySQL query statement below.
	declare colCnt int default 0;   -- 需要转换成列的个数
declare cname_i varchar(50);    -- 列名
declare colindex int default 0; -- 当前列

set @sqlStr ='select product_id ';

set colCnt=( select count(distinct store) from products);

while colCnt>colindex do
    set cname_i=(
        select 
            store
        from products
        group by store
        order by store
        limit colindex,1
    );

    set @sqlStr=concat(@sqlStr,',max(if(store=''',cname_i,''',price,null)) as ',cname_i);

    set colindex=colindex+1;
end while;

set @sqlStr =concat(@sqlStr,' from products group by product_id ');

prepare dosql from @sqlStr;
execute dosql;

END

并行球架算法的分析与实现 球架算法是一种用于重建三维曲面的无网格方法,适用于从离散的点云数据中生成三维模型。而并行球架算法是对传统球架算法的改进,在计算效率和速度上有所提升。本文对并行球架算法进行了分析和实现。 首先,对并行球架算法进行了详细的理论分析。该算法基于局部的几何特征,采用迭代的方式构建球架模型。在分析中,我们研究了算法的工作流程、算法参数的选择、局部几何特征的计算方法等。通过理论分析,我们深入了解了并行球架算法的原理和特点。 接下来,我们实现了该算法的并行版本。为了利用多核处理器的计算能力,我们采用了多线程编程技术,将球架算法中的一些计算过程并行化。通过合理的任务分配和数据交换,我们有效地利用了多核处理器的各个核心,提高了算法的计算速度。 在实验中,我们使用了多个数据集进行测试,并与传统的串行球架算法进行了对比。结果显示,并行球架算法相较于传统算法,能够更快地生成三维模型,并且保持了相同的准确性。这证实了并行球架算法在计算速度上的优势。 综上所述,本文对并行球架算法进行了深入分析与实现。分析阐述了该算法的原理和特点,并通过实现多线程编程的方式加速了算法的计算过程。实验结果表明,并行球架算法在加速计算和保持准确性方面具有优势,具有很大的应用潜力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值