我有一个MySQL表格的股票数据组织如下:
ddate idnum open high low close volume
------------------------------------------
2001 1423 1 3 0 2 100
2002 1423 3 2 1 2 150
2003 1423 NaN 4 1 3 170
2001 9486 9 10 4 8 1000
2002 9486 7 9 3 9 1200
2003 9486 8 9 5 7 1500
我想创建一个像这样组织的表openmatrix:
1423 9486
____________
2001| 1 9
2002| 3 7
2003| NaN 8
其中仅包含原始数据表中的未结价格,IDNUM对应于列,每行对应一个日期。这是我的代码:
SET @@group_concat_max_len = 1000000;
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(idnum = ',
idnum,
',open, NULL)) AS ''',
idnum,''''
)
) INTO @sql
FROM rawdatatable;
SET @sql = CONCAT('SELECT ddate , ', @sql, ' FROM rawdatatable GROUP BY ddate');
SELECT @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;
有更有效的方法吗?