I can't create this procedure - mysql

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
$$

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

Joining with dynamic sql

I have the following dynamic sql query which works as intended. But now I want to join the result with the result of another query on account_id. I tried the usual way by making the query a derived table then joining but this doesn't work (I assume it has to do with all the semi-colons). Is there a special way to join a dynamic sql query with another query ?
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'sum(CASE WHEN phone_name = ''',
phone_name,
''' THEN 1 else 0 END) AS `',
phone_name, '`'
)
) INTO #sql
FROM yt;
SET #sql
= CONCAT('SELECT account_id, ', #sql, '
from yt
group by account_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Can you simply put the rest of your query in your concatenation?
...
/* Your existing code */
SET #sql
= CONCAT('SELECT account_id, ', #sql, '
from yt
group by account_id');
/* Now put your derived table stuff in */
SET #sql
= CONCAT('select a.account_id, b.something_else
from some_table as b join (', #sql, ') as a
on a.account_id = b.account_id');
/* Continue your existing code */
PREPARE stmt FROM #sql;
...