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;
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 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.
I've looked through a lot of posts on this site and others and I can't figure this out. I'm trying to select a list of columns from a table and then use them in a query similar to this:
set #cols = (select group_concat(column_name) as 'col_list' FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'REPORTS' AND `TABLE_NAME` = 't_labor' AND column_name like '%host%'));
set #s = 'select ' + #cols + ' from REPORTS.t_labor';
prepare stmt from #s;
execute stmt;
deallocate prepare stmt;
-- execute ('select ' + #cols + ' from REPORTS.t_sales');
I am trying to run the above statements as-is. I also tried creating a stored procedure, but I have never worked with stored procedures in MySQL before and I don't know how to debug them. I would prefer to do this without using a stored procedure if possible, but if it's necessary that's ok.
I tried using the prepare statement, and I tried the execute statement but I couldn't get either to work.
Wild guess here, but try this.
set #cols = (select group_concat(column_name) as 'col_list' FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'REPORTS' AND `TABLE_NAME` = 't_labor' AND column_name like '%host%'));
PREPARE stmt1 FROM 'select ? from REPORTS.t_labor';
EXECUTE stmt1 USING #cols;
DEALLOCATE PREPARE stmt1;
Finally figured it out:
set #cols = (select group_concat(column_name) as 'col_list' FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'REPORTS' AND `TABLE_NAME` = 't_labor' AND column_name like '%host%'));
set #qry = concat('select ', #cols, ' from REPORTS.t_labor');
PREPARE stmt1 FROM #qry;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Had to piece together replies from at least 5 different posts similar to this question plus Dr OSWaldo's reply.
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;
PSEUDO:
select * from foo where [every column is not null]
Is there a way to do this without specifying the actual column names?
A possible solution that I can think of involves using dynamic-SQL
SELECT GROUP_CONCAT(column_name SEPARATOR ' IS NOT NULL AND ')
INTO #sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = SCHEMA()
AND table_name = 'foo'
GROUP BY table_name;
SET #sql = CONCAT('SELECT * FROM foo WHERE ', #sql, ' IS NOT NULL');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Here is SQLFiddle demo
Obviously it can be wrapped into a stored procedure with a parameter for a table name
DELIMITER $$
CREATE PROCEDURE sp_select_all_not_null(IN tbl_name VARCHAR(64))
BEGIN
SELECT GROUP_CONCAT(column_name SEPARATOR ' IS NOT NULL AND ')
INTO #sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = SCHEMA()
AND table_name = tbl_name
GROUP BY table_name;
SET #sql = CONCAT('SELECT * FROM ', tbl_name, ' WHERE ', #sql, ' IS NOT NULL');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
DELIMITER ;
And then use it
CALL sp_select_all_not_null('foo');
Here is SQLFiddle demo for that