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;
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 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
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"
in reference to this [pivot article]
I managed to get this prepared pivot query
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(IF(name = ''',
name,
''', 1, NULL)) AS ',
name
)
) INTO #sql
FROM bundles;
SET #sql = CONCAT('SELECT ', #sql, ' FROM bundles');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
and here's a live demo SQLfiddle
the problem is when I try to execute that on my data with the same schema using mysql(5.6.14 Win32 x86) I get an error.
Error
SQL query:
PREPARE stmt FROM #sql ;
MySQL said:
#1064 -
just an error code but no message..
I've read SO questions like this & this but the answers are the static way which won't work with unknown columns
first of all, is this even available in mysql?? .. any pointers are appreciated
Your schema may be the same, but your data is probably different. You may have a keyword, space, or something else in your values which is causing the issue. Try to wrap your alias in double quotes.
Here's my edit to your SQLFiddle. I added a line which outputs the dynamic SQL in case you need to examine what you get on your system with your data.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(IF(name = ''',
name,
''', 1, NULL)) AS "',
name, '"'
)
) INTO #sql
FROM bundles;
SET #sql = CONCAT('SELECT ', #sql, ' FROM bundles');
SELECT CONCAT(#sql);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
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