I have got a vast stock data in mysql in the following format...
date symbol open high low close volume
2017-11-07 stockA 8.9 8.92 8.88 8.9 68987
2017-11-08 stockA 8.91 8.95 8.9 8.92 98793
2017-11-09 stockA 8.75 8.88 8.71 8.77 99871
2017-11-07 stockB 18.9 18.92 18.88 18.9 12109
2017-11-08 stockB 18.91 18.95 18.9 18.92 11898
2017-11-09 stockB 18.75 18.88 18.71 18.77 10898
2017-11-07 stockC 3.9 3.92 3.88 3.9 98093
2017-11-08 stockC 3.91 3.95 3.9 3.92 109871
2017-11-09 stockC 3.75 3.88 3.71 3.77 139405
...
I am looking to create a table that gathers the types of prices as rows and of all stocks in the database as columns:
date symbol open high low close volume
2017-11-07 stockA 8.9 8.92 8.88 8.9 68987
2017-11-08 stockA 8.91 8.95 8.9 8.92 98793
2017-11-09 stockA 8.75 8.88 8.71 8.77 99871
2017-11-07 stockB 18.9 18.92 18.88 18.9 12109
2017-11-08 stockB 18.91 18.95 18.9 18.92 11898
2017-11-09 stockB 18.75 18.88 18.71 18.77 10898
2017-11-07 stockC 3.9 3.92 3.88 3.9 98093
2017-11-08 stockC 3.91 3.95 3.9 3.92 109871
2017-11-09 stockC 3.75 3.88 3.71 3.77 139405
...
date type stockA stockB stockC stockD ...
2017-11-07 open 8.9 18.9 3.9
2017-11-08 open 8.92 18.92 3.92
2017-11-09 open 8.77 18.77 3.77
2017-11-07 high 8.9 18.92 3.92
2017-11-08 high 8.95 18.95 3.95
2017-11-09 high 8.88 18.88 3.88
2017-11-07 low 8.88 18.88 3.88
2017-11-08 low 8.9 18.9 3.9
2017-11-09 low 8.71 18.71 3.71
2017-11-07 close 8.9 18.9 3.9
2017-11-08 close 8.92 18.92 3.92
2017-11-09 close 8.77 18.77 3.77
I'm a newbie to mysql/ sql syntax... so i'm totally clueless about how i could go about to do this... has anyone done anything similar?
thanks all for the links etc.. I tried to tackle the problem with the following script after reading the links... somehow it threw a SQL syntax error (error code:1064). It looks like the script stopped after sorting for the first 2 stocks, then there was a mismatch of symbol...
SET @sql = NULL;
SELECT
GROUP_CONCAT( DISTINCT
CONCAT(
'MAX( if( symbol="',
symbol,
'" , open, NULL )) as "',
symbol, '"'
)
) INTO @sql
FROM prices ;
SET @sql = CONCAT(' SELECT date, ', @sql, '
FROM prices');
PREPARE stmt from @sql ;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;