SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('sum(case when week = ',week,
'then totaltime else 00 end) AS `',
week, '`')
)INTO #sql
FROM mytable;
SET #sql = CONCAT('SELECT TRIM(UPPER(mytable.empcode))AS CODE,
TRIM(UPPER(mytable.empname)) EMPNAME,
', #sql, '
from mytable
group by empcode
order by empname ');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
I am using pivot table here, want to make sum of totaltime according to week field which is dynamically generated by query, but I got an error :
"#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 mytable group by empcode or' at line 6"
Related
What I have:
I have the following query that gets me pretty much what I need:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(Date = ''',
Date,
''', Description, NULL)) AS ',
CONCAT("'",Date,"'")
)
) INTO #sql
FROM updates;
SET #sql = CONCAT('SELECT Action, ', #sql, ' FROM updates GROUP BY Action');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
And gives the following output:
What I need:
What I would like is for the Column headers to be in the following format examples:
day/month/year
04/10/2016
%d/%m/&Y
What I have tried:
I have tried a number of different alterations to my code (far too many minor changes to list here) but each time I seem to get an error, to which I am guessing is regarding the formatting/syntax of the query.
Below is one example I tried to show the wave of thinking I was on:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(Date = ''',
Date,
''', Description, NULL)) AS ',
CONCAT("'",Date_Format(Date, "'"%d/%m/%Y"'"),"'")
)
) INTO #sql
FROM updates;
SET #sql = CONCAT('SELECT Action, ', #sql, ' FROM updates GROUP BY Action');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
As you can see I tried to set the "AS" value (Column name) using the DATE_FORMAT snippet of code. However, when I run this, I get the following error after the "FROM updates;" and before the "INTO #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 '%m/%Y"'"),"'")
This in turn gives me the following error message after the 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 'NULL' at line 1
If anyone has any idea how to help me with this or point me in the right direction, I will be very grateful!
This select concat(date_format(now(), '%d/%m/%Y')) gives me 11/10/2016.
This is working perfectly fine. I guess you should consider removing the double-quotes. That should do the trick.
I got this error when tried to execute this:
#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
Can't seems to find what is the problem. Appreciate if anyone can help
SET #sql = NULL;
SELECT
GROUP_CONCAT(
DISTINCT CONCAT (
"SUM(IF(DATE(FROM_UNIXTIME(machine_stop)) = '",
DATE(FROM_UNIXTIME(machine_stop)),"' ,
(machine_start-machine_stop)/3600, 0)) AS ",
DATE(FROM_UNIXTIME(machine_stop))
)
) INTO #sql
FROM
downtime_data
WHERE
DATE(FROM_UNIXTIME(machine_stop)) >= DATE(NOW()) - INTERVAL 7 DAY;
SET #sql = CONCAT("SELECT
failure_code, ", #sql, "
FROM
downtime_data
WHERE
p.machine='HH1' AND
DATE(FROM_UNIXTIME(machine_stop)) >= DATE(NOW()) - INTERVAL 7 DAY
GROUP BY
failure_code,
DATE(FROM_UNIXTIME(machine_stop))"
);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
If by any chance the #sql variable still holds NULL value after the first select statement then you are going to encounter an exception later on while executing the prepare statement.
Look at the following select statement using CONCAT
SET #sql := NULL; SELECT CONCAT('abc',#sql,'def');
The result is NULL. Although you might expect the result to be abcdef.
In order to get abcdef you need to do this
SET #sql := NULL; SELECT CONCAT('abc',IFNULL(#sql,''),'def');
You may try any of the following if it resolves the issue:
Either
1) SET #sql := '';
OR
2) If you want to keep this line SET #sql = NULL; then change the portion of the final query like this SET #sql = CONCAT("SELECT failure_code ", IF(#sql IS NULL, '',CONCAT(',',#sql)),
Here's the final query:
SET #sql = CONCAT("SELECT
failure_code ", IF(#sql IS NULL, '',CONCAT(',',#sql)), "
FROM
downtime_data
WHERE
p.machine='HH1' AND
DATE(FROM_UNIXTIME(machine_stop)) >= DATE(NOW()) - INTERVAL 7 DAY
GROUP BY
failure_code,
DATE(FROM_UNIXTIME(machine_stop))"
);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I got it, there are no row from the query under this condition
DATE(FROM_UNIXTIME(machine_stop)) >= DATE(NOW()) - INTERVAL 7 DAY
So it returned NULL
I have following code. I am getting an error like:
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
MySQL code:
set session group_concat_max_len = 8192*50;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'select id, "',
c.column_name,
'" as word from limesurvey.lime_survey_697389 '
) SEPARATOR ' UNION ALL '
) INTO #sql
FROM information_schema.columns c
where c.table_name = 'limesurvey.lime_survey_697389'
and c.column_name like '697389%'
order by c.ordinal_position;
SET #sql
= CONCAT('select id, word
from
(', #sql, ') x order by id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Can anyone help here? I am new to MySQL.
You cannot define NULL value to #sql. Instead you can try like this:
SET #sql = '';
EDIT:
You need to execute the above code inside a stored program or function. The MYSQL docs says:
MySQL supports the IF, CASE, ITERATE, LEAVE LOOP, WHILE, and REPEAT
constructs for flow control within stored programs.
Is it possible that the 9.4 User-Defined Variables #sql has NULL?
Try:
...
SET #`sql` := (SELECT IF(
#`sql` IS NULL, 'SELECT \'#`sql` IS NULL\' `error`',
CONCAT('SELECT `id`, `word`
FROM (', #`sql`, ') `x` ORDER BY `id`')
));
...
SQL Fiddle demo
I am trying to create a pivot table view with below stored procedure. It worked on Mac OS X, MySQL ver 5.6.16. But when I created the same procedure on Windows, MySQL ver 5.1.45, there's error message:
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
Stored Procedure:
DELIMITER $$
CREATE DEFINER=`crossover`#`localhost` PROCEDURE `getPatientTestRecordsEid`(IN varPid int, IN varTid int)
BEGIN
SET #sql = NULL;
SET #myPid = varPid;
SET #myTid = varTid;
SELECT GROUP_CONCAT(DISTINCT
CONCAT('MAX(CASE WHEN TestDate = ''', TestDate,
''' THEN TestResult END) `', TestDate, '`'))
INTO #sql
FROM
(select
date_format(Test_Record.Date, "%Y-%m-%d") As TestDate,
Test_Item.EID AS TestItem,
Test_Record_Detail.Result AS TestResult
from
Test_Record, Test_Record_Detail, Test_Item
where
Test_Record.TR_ID = Test_Record_Detail.TR_ID AND
Test_Item.EID = Test_Record_Detail.EID AND
Test_Record.PID = #myPid AND
Test_Record.TID = #myTid) As t;
SET #sql = CONCAT('SELECT TestItem, ', #sql, '
FROM
(select
date_format(Test_Record.Date, "%Y-%m-%d") As TestDate,
Test_Item.EID AS TestItem,
Test_Record_Detail.Result AS TestResult
from
Test_Record, Test_Record_Detail, Test_Item
where
Test_Record.TR_ID = Test_Record_Detail.TR_ID AND
Test_Item.EID = Test_Record_Detail.EID AND
Test_Record.PID = #myPid AND
Test_Record.TID = #myTid) As t
GROUP BY TestItem');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
I am new to php and mysql and I am trying to create a crosstab query using the code below:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(IF(category = ''',category,''', duration,NULL))AS ',category)) INTO #sql
FROM tblnon_oeedata;
SET #sql = CONCAT('SELECT productionDay, ', #sql, ' FROM tblnon_oeedata GROUP BY productionDay');
PREPARE stmt FROM #sql;
EXECUTE stmt;
Everytime I run it I keep getting the error:
#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 'Maintenance During Production,SUM(IF(category = 'Breakdowns', duration,NULL))AS ' at line 1
How do I solve this problem, i am really stuck. Thanks in Advance
Try this:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(IF(category = ''',category,''', duration,NULL))AS ''',category, '''')) INTO #sql
FROM tblnon_oeedata;
SET #sql = CONCAT('SELECT productionDay, ', #sql, ' FROM tblnon_oeedata GROUP BY productionDay');
PREPARE stmt FROM #sql;
EXECUTE stmt;