put results of execute statement into table in MySQL - mysql

At MySQL pivot efficiency
I was wondering how to pivot a table of stock prices in MySQL. The consensus seems to be that this is not a good thing to do. I originally wanted each column to be an ID number and each row to be a date. I got much more speed by making each column a date and each row an ID number:
SET ##group_concat_max_len = 1000000;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(ddate = ',
'ddate',
',closing_price, NULL)) AS ''',
DATE_FORMAT(ddate,'%Y%m%d'),''''
)
) INTO #sql
FROM rawdatatable;
SET #sql = CONCAT('SELECT idnum , ', #sql, ' FROM rawdatatable GROUP BY idnum');
SELECT #sql;
PREPARE stmt FROM #sql;
EXECUTE stmt;
Is there any way to put the results of the EXECUTE statement into a table, where each column name is a different ddate?

Change this line
SET #sql = CONCAT('SELECT idnum , ', #sql, ' FROM rawdatatable GROUP BY idnum');
to
SET #sql = CONCAT('CREATE TABLE blah AS SELECT idnum , ', #sql, ' FROM rawdatatable GROUP BY idnum');

Try to put the result into a temp table like #Desolator say. Something like this in MySQL console
mysql>CREATE TABLE temp_result AS
SELECT * FROM rawdatatable
WHERE ...;

Related

Mysql query only showing rows affected and not the table

The following query is only returning rows affected and not the table.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when department = ''',
department,
''' then questionValue end) ',
department
)
) INTO #sql
FROM
jos_survey_submission_values;
SET #sql = CONCAT('SELECT questionNumber AS "", category AS Category, dimension AS Dimension, subDimension AS `Sub-Dimension`, ', #sql, '
FROM jos_survey_submission_values
WHERE company_id = "180"
GROUP BY category, dimension, subDimension, questionNumber
ORDER BY questionNumber ASC');
PREPARE stmt FROM #sql;
EXECUTE stmt;
What can I do to get it to return the table?

Is it possible to UNION ALL a prepared statement and a select statement?

I am trying to write up something that can be run once and exported with all the data from a prepared statement and a select statement. Is it possible to union them?
example dynamic pivot table
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'format(sum(case when field_key = ''',
field_key,
''' then field_value end), 0) ',
field_key
)
) INTO #sql
FROM
Meeting;
SET #sql = CONCAT('SELECT Meeting_id, ', #sql, '
FROM Meeting
GROUP BY Meeting_id'
);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
example select statement
set #startdate = '2020-11-10'
set #enddate = '2020-12-30'
select column1,
column2,
column3,
if(col.id = id,1,0) column4,
if(col.id = id,0,1) column5
from Meeting
group by col.id
order by column2;
If its not possible to UNION ALL them then what would be your suggestions?
Unions have to have the same number of columns (and their names are taken from the first query in the union), and here the pivot query has a dynamic number. You would need to adjust the columns of your second select to match and add it to your #sql as a union.
Often with unions you add a type field that is hardcoded and different for each of the unioned queries, and have each query return null for columns that aren't relevant to that query.

Generate result in CSV file using prepared statement query in laravel

I have created this query to generate report and its working fine but i want to download the result of this query in .CSV file. How to execute this query in laravel ?
Query :
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when wm.SubPointId = ''',
wm.SubPointId,
''' then Balance else 0 end) AS ',
REPLACE ( ws.SubPointType, ' ', '' )
)
) INTO #sql
FROM wallet_master wm, wallet_subpoints ws WHERE wm.SubPointId = ws.SubPointId;
SET #sql = CONCAT('SELECT MobileNo, ', #sql, ', sum(Balance) as `TotalBalance`
FROM wallet_master wm
GROUP BY MobileNo');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

MYSQL pivoting rows and limiting by rows of a specific column

Not entirely sure how to word the question so I gave it my best show and am hoping that the explanation will help get the point across. Any questions, please ask.
I have the following setup for my database:
create table spooner_pec
(
id int,
year varchar(20),
policy_number int,
primary_name varchar(200),
experience_rate decimal(10,2),
mco_name varchar(200)
);
with these values being inserted:
insert into spooner_pec values
(1,'2009',183586,'ZBIN LANDSCAPING INC', 1.22, 'GENEX CARE FOR OHIO'),
(1,'2011',183586,'ZBIN LANDSCAPING INC', 0.93, 'COMPMANAGEMENT HEALTH SYSTEMS, INC.'),
(1,'2012',183586,'ZBIN LANDSCAPING INC', 0.92, 'HEALTH MANAGEMENT SOLUTIONS, INC.'),
(1,'2013',183586,'ZBIN LANDSCAPING INC', 0.50, 'CAREWORKS'),
(1,'2014',183586,'ZBIN LANDSCAPING INC', 0.47, 'UNIVERSITY HOSPITALS COMPCARE'),
(1,'2010',183586,'ZBIN LANDSCAPING INC', 1.27, 'SHEAKLEY UNICOMP')
The query that I have working so far is this:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when year = ',
year,
' then experience_rate end) AS `',
year, '-Pen`'
) ORDER BY year
) INTO #sql
FROM
spooner_pec;
SET #sql = CONCAT('SELECT policy_number, primary_name, ', #sql, '
FROM spooner_pec
GROUP BY policy_number');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
So I have one row for each company and then a year-pen column for each year. What I need to do now is only get the rows where the penalty of the latest year is greater than 1, for example. So if the last year entered for all the data was 2014, I need where 2014-Pen, in this case, is > 1.
SQLFiddle
Do another query to get the last year, and merge that into the SQL.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when year = ',
year,
' then experience_rate end) AS `',
year, '-Pen`'
) ORDER BY year
) INTO #sql
FROM
spooner_pec;
SELECT MAX(year) INTO #lastYear FROM spooner_pec;
SET #sql = CONCAT('SELECT policy_number, primary_name, ', #sql, '
FROM spooner_pec
GROUP BY policy_number
HAVING `', #lastYear, '-Pen` > 1');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

how to select only non empty columns of a table from database

I have a table with 46 columns in my database, In these 46 columns 25 or 26 are mostly empty.
when I select all columns by some condition I want only those column will select which have some data in it
You can try this query to show you the not null columns' names:
SET group_concat_max_len = 4294967295;
SELECT GROUP_CONCAT(CONCAT(
' SELECT ',QUOTE(COLUMN_NAME),
' FROM table_name',
' WHERE `',REPLACE(COLUMN_NAME, '`', '``'),'` IS NOT NULL',
' HAVING COUNT(*)'
) SEPARATOR ' UNION ALL ')
INTO #sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'table_name';
PREPARE stmt FROM #sql;
EXECUTE stmt;
SQL Fiddle
And then just excute these columns manualy:
SELECT column_name3, column_name8 FROM Table_Name;
SQL Fiddle