Hei everyone:)
I am trying to create a pivot table locally on mysql. I tried to implement the solutions from here however I was not able to implemnt it so far. I also watched and did a bunch of tutorials but with the time I only got more confused... I already countless hours respective days on it and I am getting now a bit desperat... So I was wondering if you might help what I do wrong respectively how that I can create my pivot table...
I have currently the following table "act_gen_raw" from where I want to take the data from and two control tables "plant_information" and "dt". "plant_information" stores the distinct plant names (PowerSystemResourceName) and "dt" the distinct datetimes which I have collected from the "act_gen_raw" to control if all data are as they should be.
The "act_gen_raw" tables looks something like this:
Datetime PowerSystemResourceName ActualGenerationOutput
2014-12-10 20:00:00 Stalon G1 29.2
2014-12-11 01:00:00 Miranda - G3 0
2014-12-10 20:00:00 CSP_GT1 0.1
2014-12-11 01:00:00 Stalon G1 0
2014-12-11 01:00:00 CSP_GT1 120.5
2014-12-10 20:00:00 Miranda - G3 139.0
I put also some datasamples of my tables as mysqldumps here. I would like to achieve the following table:
Datetime Stalon G1 Miranda - G3 CSP_GT1
2014-12-10 20:00:00 29.2 139.0 0.1
2014-12-11 01:00:00 0 0 120.5
I have however 2153 distinct plant names (PowerSystemResourceName) and 243030 distinct Datetimes variables. How can I now write a mysql querie such that I can create my desired table without manually write each column name?
If possible I would also like to make sure that all that all plant names (PowerSystemResourceName) matches there counterpart in the table "plant_information" and each datetime matches one in the "dt" table.
Best, David
UPDATE
I currently trying to solve this problem with the following script where I put the name for the first step manually:
drop table if exists plants_production;
CREATE TABLE plants_production AS(
SELECT
tab2.Date_and_Time,
CASE
WHEN PlantName = 'Stalon G1' THEN gen
END AS 'Stalon G1',
CASE
WHEN PlantName = 'Miranda - G3' THEN gen
END AS 'Miranda - G3',
CASE
WHEN PlantName = 'CSP_GT1' THEN gen
END AS 'CSP_GT1'
FROM (SELECT Date_and_Time, gen, PlantName FROM (
SELECT
dt.datetime_variable AS Date_and_Time,
act_gen_raw_test.ActualGenerationOutput AS gen,
act_gen_raw_test.PowerSystemResourceName AS PlantName
FROM
dt, act_gen_raw_test
) tab1
WHERE tab1.PlantName IN (SELECT
plant_information.PowerSystemResourceName
FROM plant_information)
) tab2);
However I get the output in this form:
Datetime Stalon G1 Miranda - G3 CSP_GT1
2014-12-06 23:00:00 0.1
2014-12-06 23:00:00 121
2014-12-06 23:00:00 59.5
How can I make sure that I only get one row for each Datetime?
UPDATE 2- dynamic query does not run I tried to implement the query according to the proposed example:
drop table if exists plants_production;
CREATE TABLE plants_production AS(
SELECT GROUP_CONCAT(DISTINCT
act_gen_raw_test.PowerSystemResourceName)
INTO @sql
FROM act_gen_raw_test;
SET @sql = CONCAT('
SELECT
tab2.Date_and_Time, gen ', @sql,'
FROM (
SELECT Date_and_Time, gen, PlantName FROM (
SELECT
dt.datetime_variable AS Date_and_Time,
act_gen_raw_test.ActualGenerationOutput AS gen,
act_gen_raw_test.PowerSystemResourceName AS PlantName
FROM
dt, act_gen_raw_test
) tab1
WHERE tab1.PlantName IN (SELECT plant_information.PowerSystemResourceName FROM plant_information)
) tab2'
#end my select query
)
);
#prepares statment for execution
PREPARE stmt FROM @sql;
#executes statmen
EXECUTE stmt;
#deallocate statment
DEALLOCATE PREPARE stmt;
I am however unable to run it due to the following error when I try to run PREPARE stmt FROM @sql;
:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1