GROUP_CONCAT mysql statement error - mysql

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

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

Write dynamic pivot in 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.

ERROR IN parameter passing in pivot table mysql

UPDATED:
I am using pivot table for displaying column names as header as per my knowledge everything is fine but when i tried to add parameter I am getting error while
calling
call sp_schreport(1,2,'2014-10-04'); $$
AND WHEN I TRIED TO ADD
"select venue,'null' as first,'null' as second,'null' as third,'null' as fourth from sch_venue where campusid =campid and venueid NOT IN (select venueid from sch_taskassigned where assignedstatus=status AND (sdate BETWEEN taskstartdate AND taskenddate)) UNION"
along with that code getting error for union function both query column should be equal so that only i have used 'null' first etc..
CREATE PROCEDURE sp_schreport(IN campid int,IN status int,IN sdate datetime)
BEGIN
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(CASE WHEN t.session = ''',
t.session,
''' THEN d.department END) AS `',
tagname, '`'
)
) INTO #sql
FROM sch_sessions t;
SET #sql
= CONCAT('select venue,'null' as first,'null' as second,'null' as third,'null' as fourth from sch_venue where campusid =campid and venueid NOT IN (select venueid from sch_taskassigned where assignedstatus=status AND (sdate BETWEEN taskstartdate AND taskenddate))
UNION SELECT v.venueid, ', #sql, '
from sch_taskassigned p
left join sch_sessions t
on p.sid = t.sid inner join sch_venue v on p.venueid=v.venueid inner join sch_departments d where p.campusid=campid and p.assignedstatus=status and (sdate BETWEEN taskstartdate AND taskenddate) group by p.venueid');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
Kindly find out the mistakes in my procedure

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