Write dynamic pivot in MYSQL - mysql

This is my code for pivot my data. I want to convert it to dynamic..
I tried and it showing error..
SELECT id,
GROUP_CONCAT(
CASE
WHEN question.question_name = 'Household'
THEN answer.text
ELSE NULL
END
) AS Household,
GROUP_CONCAT(
CASE
WHEN question.question_name = 'Dependents'
THEN answer.text
ELSE NULL
END
) AS Dependents,
GROUP_CONCAT(
CASE
WHEN question.question_name = 'Generation'
THEN answer.text
ELSE NULL
END
) AS Generation
FROM user_answers
inner join answer on user_answers.answer_id=answer.answer_id
inner join question on answer.question_id=question.question_id
GROUP BY id
the code is working.. I want to convert it into dynamic conversion. I tried. but not working
SET #sql = NULL;
SELECT
GROUP_CONCAT(
'case when question.question_name = ''',
question.question_name,
''' then answer.text ELSE 0 end) AS `',
question_name, '`'
) INTO #sql
FROM answer
inner join question on answer.question_id=question.question_id;
SET #sql = CONCAT('SELECT id, ', #sql, '
FROM user_answers
inner join answer on user_answers.answer_id=answer.answer_id
inner join question on answer.question_id=question.question_id
GROUP BY id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
showing the error
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 ') AS `Dependents`,case when question.question_name = 'Dependents' then answer.te' at line 1 0.000 sec

The code in this blog will generate the desired query and then execute it:
http://mysql.rjweb.org/doc.php/pivot
A Stored Procedure must be used -- that is what was causing the error you got.

Related

dynamic pivot table in mysql and join two table

i have two table in mysql in the names of dapa_collector_tag_value d and dapa_collector_tag t i want to show dynamic pivot table
enter image description here
this is my code
SET #sql = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT('SUM(CASE WHEN d.tag_id = ''',
d.tag_id, ''' THEN d.tag_value ELSE 0 END) AS ''', d.tag_id, '''')
INTO #sql
FROM dapa_collector_tag_value d
INNER JOIN dapa_collector_tag t ON d.tag_id = t.id;
SET #sql = CONCAT(
'SELECT d.effective_date, ', #sql, ' ',
'FROM dapa_collector_tag_value d ',
'INNER JOIN dapa_collector_tag t ON d.tag_id = t.id ',
'GROUP BY d.effective_date'
);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
i have error SQL Error [1064] [42000]: 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 'FROM dapa_collector_tag_value d
INNER JOIN dapa_collector_tag t ON d.tag_id = t.' at line 1
Subqueries need to be in parens: ( SELECT ... ).
Your code needs to be inside a Stored Procedure; let's see the whole SP.
Please use {} for formatting code, not ".
INTO NULL does not make sense.

Error Code: 1064 - MY SQL : DYNAMIC ROWS TO COLUMN

SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(case when subject_details.subject_Name = ''',
subject_details.subject_Name,
''' then (marks.internal_Marks+marks.external_Marks) End) AS ',
REPLACE(subject_details.subject_Name, ' ', '')
)
) INTO #sql
FROM subject_details;
SET #sql = CONCAT('SELECT stud.student_Name,',#sql , 'FROM marks
INNER JOIN student_details AS stud ON stud.pkey = marks.fk_Student_Name
INNER JOIN subject_details ON subject_details.pkey = marks.fk_Subject_Name
INNER JOIN dept_name ON dept_name.pkey = subject_details.fk_Dept_Name
GROUP BY stud.student_Name');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
When i run the code I get this error:
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 'FROM marks
INNER JOIN student_details AS stud ON stud.pkey = marks.fk_Student_Na' at line 1

I can't create this procedure

I tried to create new stored procedure with this code, this error always appear to me:
CREATE PROCEDURE `retriveData`() NOT DETERMINISTIC CONTAINS SQL SQL
SECURITY DEFINER BEGIN
SET #sql = NULL
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near '' at line 4
I used this code to create stored procedure:
CREATE PROCEDURE `retriveData`() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER
BEGIN
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(pa.item = ''',
item,
''', pa.comValue, 0)) AS ',
item
)
) INTO #sql
FROM salaryStructureView;
SET #sql = CONCAT('SELECT p.id
, p.fullname
, p.salary, ', #sql, '
FROM employee p
LEFT JOIN salaryStructureView AS pa
ON p.id = pa.id
GROUP BY p.id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
You forgot a delimiter:
DELIMITER $$
CREATE PROCEDURE `retriveData`() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER
BEGIN
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(pa.item = ''',
item,
''', pa.comValue, 0)) AS ',
item
)
) INTO #sql
FROM salaryStructureView;
SET #sql = CONCAT('SELECT p.id
, p.fullname
, p.salary, ', #sql, '
FROM employee p
LEFT JOIN salaryStructureView AS pa
ON p.id = pa.id
GROUP BY p.id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
$$

prepared query shows error

I am trying to make a prepared statement. I want to generate dynamic column for 'Cabang'. This is my prepared statement:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'coalesce((max(CASE WHEN Cabang.ID_Cabang = ''',
Cabang.ID_Cabang, ''' THEN PWT END)),0) AS `',
Cabang.cabang,'`'
)
) INTO #sql
FROM keyperformanceindicator
INNER JOIN cabang ON keyperformanceindicator.ID_Cabang = cabang.ID_Cabang;
SET #sql = CONCAT(
SELECT #sql,
FROM `keyperformanceindicator`
INNER JOIN pilot ON keyperformanceindicator.ID_Pilot = pilot.ID_Pilot
INNER JOIN cabang ON keyperformanceindicator.ID_Cabang = cabang.ID_Cabang
INNER JOIN
(
SELECT
DATE_FORMAT( PilotStationDate, '%m' ) AS MONTH ,
FORMAT( SUM(`PilotWaitingTime`) / COUNT('PilotStationDate'), 3 ) AS PWT,
COUNT( 'PilotStationDate' ) AS jumlah
FROM keyperformanceindicator
INNER JOIN pilot ON keyperformanceindicator.ID_Pilot = pilot.ID_Pilot
GROUP BY DATE_FORMAT( PilotStationDate, '%m' )
)x ON DATE_FORMAT( PilotStationDate, '%m' ) = x.Month
GROUP BY DATE_FORMAT(PilotStationDate, '%m')
ORDER BY UNIX_TIMESTAMP( CONCAT_WS( '', PilotStationDate, ' ', PilotStationTime ) ) ASC
);
PREPARE stmt FROM #sql;
EXECUTE stmt;
-- Clear something
DEALLOCATE PREPARE stmt;
SET #sql = NULL;
it shows error like this :
ERROR 1064 (42000): 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 'SELECT #sql, FROM `keyperformanceindicator` INNER JOIN pilot ON ' at line 2
PREPARE stmt FROM #sql;
ERROR 1064 (42000): 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 'coalesce((max(CASE WHEN Cabang.ID_Cabang = 'C001' THEN PWT END)),0) AS `Belawan`,max(CASE WHEN' at line 1
Here's a fiddle
I am new in using prepared statement. Thank you for your help.

MySQL Multiple Query > storing rows into columns

I need to create a SQL query that lists the following tables. Lines from the language list as column names. Thanks
Picture table and query:
If languages are known upfront
SELECT r.id, r.parent,
MAX(CASE WHEN n.language_id = 1 THEN n.name END) cs,
MAX(CASE WHEN n.language_id = 2 THEN n.name END) en
FROM cat_route r LEFT JOIN cat_route_name n
ON r.id = n.cat_route_id
GROUP BY r.id
Here is SQLFiddle demo
If you want it to be dynamic depending on what languages you defined in language
SET #sql = NULL;
SELECT GROUP_CONCAT(CONCAT(
'MAX(CASE WHEN n.language_id = ', id, ' THEN n.name END) `', short, '`'))
INTO #sql
FROM language;
SET #sql = CONCAT(
'SELECT r.id, r.parent, ', #sql, '
FROM cat_route r LEFT JOIN cat_route_name n
ON r.id = n.cat_route_id
GROUP BY r.id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Here is SQLFiddle demo