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.
Related
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.
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
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.
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
$$
I have tried mysql ststement for dinamically rows to column by followed here with query statement:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN col = ''',
col,
''' THEN val END) as `',
col, '`'
)
)INTO #sql
FROM
(
SELECT A.id_a, D.id_c id_c,
C.students students,
CONCAT(B.`code`, '_', A.id_a) col,
CONCAT(D.value_m, ',', D.value_n) val
FROM table_a A
INNER JOIN table_d D ON A.id_a =D.id_a
INNER JOIN table_b B ON D.id_b=B.id_b
INNER JOIN table_c C ON D.id_c=C.id_c
)dd;
SET #sql = CONCAT('SELECT id_c, students,', #sql, '
FROM(
SELECT A.id_a, D.id_c id_c,
C.students students,
CONCAT(B.`code`, '_', A.id_a) col,
CONCAT(D.value_m, ',', D.value_n) val
FROM table_a A
INNER JOIN table_d D ON A.id_a =D.id_a
INNER JOIN table_b B ON D.id_b=B.id_b
INNER JOIN table_c C ON D.id_c=C.id_c
)dd
GROUP BY id_c'
);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
with result message:
Query OK, 0 rows affected
Query OK, 1 row affected
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 '', A.id_a) col,
CONCAT(D.value_m, ',', D.value_n) val
FROM table_a A ' at line 1
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 'MAX(CASE WHEN col = 'MAT_1' THEN val END) as `MAT_1`,MAX(CASE WHEN col = 'BIO_1'' at line 1
1243 - Unknown prepared statement handler (stmt) given to EXECUTE
1243 - Unknown prepared statement handler (stmt) given to DEALLOCATE PREPARE
I'm not familiar with sql statement like this, and what's wrong with that SQL QUERY
Thanks...
If you look at your code (and zoom in):
SET #sql = CONCAT('SELECT id_c, students,', #sql, '
[..]
CONCAT(B.`code`, '_', A.id_a) col,
CONCAT(D.value_m, ',', D.value_n) val
[..]
GROUP BY id_c'
);
you will see that _ and , are black, while they should be red as part of the string. That means your string ist "broken" there. So you need to escape the single quotes with '':
SET #sql = CONCAT('SELECT id_c, students,', #sql, '
[..]
CONCAT(B.`code`, ''_'', A.id_a) col,
CONCAT(D.value_m, '','', D.value_n) val
[..]
GROUP BY id_c'
);
Or use double quotes for strings that contain single quotes:
SET #sql = CONCAT('SELECT id_c, students,', #sql, "
[..]
CONCAT(B.`code`, '_', A.id_a) col,
CONCAT(D.value_m, ',', D.value_n) val
[..]
GROUP BY id_c"
);
Now the complete string is red as it should be :-)
http://rextester.com/SLMU41976