I have very limited knowledge in mySql yet i try to help myself by reading stuff over internet and experiment things. I have a situation where i need to pivot/transpose data from rows to column. I have prepared a set of statements which is working perfectly fine as SQL:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when field_name = ''',
field_name,
''' then field_value end) ',
field_name
)
) INTO #sql
FROM
wp_fluentform_entry_details;
SET #sql = CONCAT('SELECT submission_id, ', #sql, '
FROM wp_fluentform_entry_details
GROUP BY submission_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Now i want to save it as view in mysql but unable. Any guidance in right direction would be highly appreciated.
Related
As per the attached images, each orderId will have transactions and should come as output as shown in the second image.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when tiid = ''',
tiid,
''' then amount end) ' ,concat('T',tiid)
)
) INTO #sql
FROM
Test;
SET #sql = CONCAT('SELECT poid, ', #sql, '
FROM Test
WHERE amount is not null
GROUP BY poid');
PREPARE stmt FROM #sql;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;
I have used the above code but getting the output in different format as shown below.
Kindly help me to resolve this.
Thanks in Advance.
I have a procedure where i need to check the current value every time in the another query
SET #sql = NULL;
set #ct=(select count(alloc_hrs) from emp_sample where week(alloc_date)=<current_value>);
SET SESSION group_concat_max_len = 1000000;
SELECT GROUP_CONCAT(DISTINCT
(CONCAT ('MAX(CASE WHEN alloc_date=''',
alloc_date,
''' THEN #ct END) `',
year(alloc_date),week(alloc_date),
'week`'
))
)
INTO #sql
FROM emp_sample;
SET #sql = CONCAT('SELECT emp_code, ', #sql,' FROM emp_sample group by emp_code');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Is there any function like this() in mysql
If not what should I replace to get that
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 wondeing if there is something like the title says:
SELECT CASE * WHEN NULL THEN '' ELSE * END
I'd like to do the case conditioning for hundreds of columns so it would be handy if I can have it on SELECT * statement. Could someone advice me if there is something like that at all in Ms SQL Server or MySQL? Thanks
Try with dynamic SQL (MySql)
SELECT GROUP_CONCAT(CONCAT('COALESCE(`', column_name, '`,'''') `', column_name, '`'))
INTO #sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'Table1';
SET #sql = CONCAT('SELECT ', #sql, ' FROM Table1');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I am following this tutorial on my installed version of MySQL, but it's throwing me an error:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(property_name = ''',
property_name,
''', value, NULL)) AS ',
property_name
)
) INTO #sql
FROM
properties;
SET #sql = CONCAT('SELECT item_id, ', #sql, ' FROM properties GROUP BY item_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I am pasting it on SQL editor in phpMyAdmin.
I followed the suggestion. No errors shown but here's the result:
SELECT item_id
,MAX(IF(property_name = 'color', value, NULL)) AS color
,MAX(IF(property_name = 'size', value, NULL)) AS size
,MAX(IF(property_name = 'weight', value, NULL)) AS weight
FROM properties GROUP BY item_id
If you have access to a MySQL command-line, I think you'll find your SQL code is fine (as long as #sql doesn't equal NULL) and the issue is with phpMyAdmin. Another idea is to wrap the code in a stored procedure and then CALL the procedure.
You need to remove the DEALLOCATE PREPARE stmt; from your query until after the query runs.
DEALLOCATE cancels the statement before it has a chance to run.
Try this in MySQL Workbench. I had the same issue in phpMyAdmin and I tried in Workbench and it works fine.