Rewriting multiple SQL commands into a single query - mysql

I am trying to turn this statement:
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;
Into something like this:
SELECT policy_number, primary_name, (SELECT GROUP_CONCAT(DISTINCT CONCAT('max(case when year = ', year, ' then experience_rate end) AS `', year, '-Pen`') ORDER BY year))
FROM spooner_pec
GROUP BY policy_number
But as you can see by the fiddle, I am getting some strange output as a column instead of the actual columns, what am I doing wrong here?
SQLFiddle

I generally do regular expression search to get all the code in single line or selected code in single line.
Use find & replace
1. Under find section type "\n" --find all next line keywords
2. Under replace section type " " -- replace \n with one blank space
3. Tick "Use Regular Expression"
Your result will be in single line.
Before
After
Replace with section is a blank space
*Note: This approach works on Windows platform and most of the editors.

Related

Create an insert to pivot value pair values into a table

I have a value pair table that I want to use to create a member table
Based on Taryns answer to this question
MySQL pivot table query with dynamic columns
I have this code that creates selects the data, which works fine
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN wpdg_usermeta.meta_key = ''',
meta_key,
''' THEN wpdg_usermeta.meta_value END) `',
meta_key, '`'
)
) INTO #sql
FROM
wpdg_usermeta
WHERE
wpdg_usermeta.meta_key like "member_%"
;
SET #sql = CONCAT('SELECT user_id, ', #sql, '
FROM wpdg_usermeta
GROUP BY user_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
So, my question is - does anyone know how I could alter this to run an INSERT instead of a SELECT so that I could populate the new member table?
You want to create a new table with the results of the dynamic query. I think the simplest approach is to use the create table ... as select ... syntax.
This requires very few changes to your code, and allow you to create the table on the fly based on the results of the query:
SET #sql = CONCAT(
'CREATE TABLE member AS SELECT user_id, ',
#sql,
' FROM wpdg_usermeta GROUP BY user_id'
);
Note that the datatypes of the new table are inferred from the query's metadata; this might, or might no, to exactly what you want. You can check the documentation for more detaiils.

Error on dynamic column headers with mySQL

This question in regarding mySQL.
I am reading data from an .csv file that looks like this
original table
------------------------------------
Id 2018M01 2018M02 2018M03
------------------------------------
EMEA 3 1 4
ASPAC 4 5 4
ASPAC 1 2 1
expected result
---------------------
ID Month Qty
---------------------
EMEA 2018M01 3
EMEA 2018M02 1
EMEA 2018M03 4
ASPAC 2018M01 4
.......
The months column header are dynamic, that is each month there will be new months and old months will be removed. However the total number of columns will remain the same.
Hence whenever the month columns headers change I would like the SQL code to dynamically read and provide correct results without me having to manually change several parts of the code.
I have written the following code; Code is to unpivot the month columns. However I tested the code by manually making changes to the .csv file headers by changing 2018M03 to 2018M04, and rerunning the SQL code, but it still seems to print the old data . What am I doing wrong ?
Thank you. I am fairly new to SQL.
DROP TABLE IF EXISTS book;
CREATE TABLE book (
ID VARCHARACTER(10),
2018M01 decimal(4,2),
2018M02 decimal(4,2),
2018M03 decimal(4,2)
);
LOAD DATA LOCAL INFILE '/Users/blytonpereira/Desktop/Book1.csv' REPLACE INTO TABLE book
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES;
DESCRIBE book;
SELECT ID, '2018M01' AS month, 2018M01 AS qty from book
UNION ALL
SELECT ID, '2018M02' AS month, 2018M02 AS qty from book
UNION ALL
SELECT ID, '2018M03' AS month, 2018M03 AS qty from book;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'select ID, ''',
c.column_name,
''' AS month, ',
c.column_name,
' as qty
from book
where ',
c.column_name,
' > 0'
) SEPARATOR ' UNION ALL '
) INTO #sql
FROM information_schema.columns c
where c.table_name = 'book'
and c.column_name not in ('id')
order by c.ordinal_position;
SET #sql
= CONCAT('select id, month, qty
from
(', #sql, ') x order by id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
);
** Updated answer:
This solution is dynamic, whatever columns you have in table, it will populate them and extract information in required format. For sure columns have to have something in common, like they're all begins with "2018", you can change that as needed in Query.
SELECT
GROUP_CONCAT(
CONCAT(
'SELECT id, ''', COLUMN_NAME, ''' as month, ', COLUMN_NAME, ' as QTY FROM t1 ') SEPARATOR ' UNION ALL ')
FROM
`INFORMATION_SCHEMA`.`COLUMNS`
WHERE
`COLUMN_NAME` LIKE '2018%'
INTO #sql;
SET #query = CONCAT('select id, month, QTY from (' , #sql , ') x order by id;');
SELECT #query;
PREPARE stmt FROM #query;
EXECUTE stmt;
**Note: the query has 2 outputs, first is the prepared concatenated query string (just to know what it looks like before run), and the other is the actual data. If you want only actual data you can comment (SELECT #query;) or remove it.

Debugging MySQL pivot row into dynamic number of columns

I recently came across the thread below, and it was very useful in building a dynamic SQL for MySQL.
MySQL pivot row into dynamic number of columns
With that said, I did struggle with trying to debug the statement. Now for the real purpose of this post! To debug, I would run a Select on my variable containing the statement (Select #SQL). Then copy that result from the viewer windows and have the query analyzer review it. Once I did this, development really sped up. I am sure this is known by all the advance pro developers but for any newbies, I hope this help!
My dynamic statement looks like this as a reference.
SET #sql = NULL;
SET ##group_concat_max_len = 50000;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
' sum(
case when symbol = ''',
symbol,
''' then pctttlassets end) AS ',
CONCAT(UPPER(ACode),'_',REPLACE(Symbol, '+', ''))
)
) INTO #sql
from trade_detail this;
SET #sql = CONCAT('SELECT Distinct(main.port_code) as PortCode, ', #sql, '
FROM trade_detail main GROUP BY main.Port_Code');
SELECT #SQL;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Limit the columns displayed in a MYSQL pivot table

The Question:
How do I limit the number of columns displayed/produced on a MYSQL pivot table?
My Setup:
I have a table named "updates" that looks like the following:
I have the following snippet of query (This is only part of the query, the whole thing only adds more columns from other tables but this is the only section that gets pivoted):
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;
The result of this query is as follows:
As you can see, this pivots the table as intended with the dates as columns. However, there is potential for these updates (to actions) to become very long before they are "closed" and not displayed. Therefore, I would like to limit the outcome to the latest 3 updates. BUT..Not per action as this would potentially still give me a lot of updates in the pivot table.
I would like to have the most recent 3 dates from the updates table with all updates for each date keeping this pivot format.
Example: The outcome table above would look the same but with the exception of the columns titled "02/10/2016" and "04/10/2016".
Thanks in advance for any assistance or advise.
For anyone else trying to solve this issue, I managed to use the following query to produce the desired results:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(Date = ''',
Date,
''', Description, NULL)) AS ',
CONCAT("'",Date_Format(Date, '%d/%m/%Y'),"'")
) ORDER BY Date ASC
) INTO #sql
FROM (
SELECT * FROM updates
GROUP BY Date
ORDER BY Date DESC
LIMIT 2)
AS updates;
SET #sql = CONCAT('SELECT Action, ', #sql, ' FROM updates GROUP BY Action');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Execute statement only returns #Rows

I'm using PMA to test some pivot queries (dynamic columns), everything seems to be working just fine however, I'm only getting the # Rows in my results, not the actual set of rows.
How can I see my result set?
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(t.week_end = ''',
t1.week_end,
''', t.st_hours, NULL)) AS ''',
t1.week_end, '\''
)
) INTO #sql
FROM timesheets t1 WHERE t1.week_end > "2015-03-01";
SET #sql = CONCAT('SELECT t.assignment_id
, ', #sql, '
FROM timesheets t
LEFT JOIN timesheets t1 ON t.timesheet_id = t1.timesheet_id
GROUP BY t.assignment_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
Returns # Rows: 440
SELECT * FROM table - Returns the actual set of rows
This will be resolved in the latest PHPMyAdmin builds, and should be released in version 4.6.
[Prepared statements] can be sent in query as this pretty much works in phpMyAdmin right now. The only problem is displaying results. If you execute all of above, you get result just from last query (DEALLOCATE), which shows 0 rows, but if you do it without DEALLOCATE, you reportedly get 1 row, but it's not displayed.
Reference