Using unpivot in mysql with many columns
My Input looks like this:
I want to see my output like : (I am trying to do unpivot in mysql data transform in Domo)
Product Type , Date, Revenue
A , 12-31-2015, 100
B,12-31-2015, 0
I am trying to use stored procedure:
create PROCEDURE unpivot_cols()
BEGIN
SET @sql = NULL;
SELECT GROUP_CONCAT(DISTINCT
CONCAT('select `Product Type`, ' '''', COLUMN_NAME, ''' col, ', column_name, ' as value from testing_unpivot'
) separator ' union all '
) INTO @sql
FROM INFORMATION_SCHEMA.COLUMNS
where table_name = 'testing_unpivot'
and column_name <> 'Product Type';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
When i try to execute above procedure , Isee no output. Can you help me?
Best Answer
-
I got this one finally. Below is the sql code
Step 1
SELECT GROUP_CONCAT(
CONCAT(
' SELECT `Product Type` , ' ,QUOTE(COLUMN_NAME) , ' AS `Date`, ','`', COLUMN_NAME , '` AS `Revenue` ',
'FROM testing_unpivot'
) SEPARATOR ' UNION ALL '
) as product_info
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'testing_unpivot'
AND COLUMN_NAME <> 'Revenue Type';Step 2
CREATE PROCEDURE procedure_new()
BEGIN
SELECT product_info into @sql2 from transform_data_1;
SET @str=concat('create table products as ',@sql2);
PREPARE q from @str;
EXECUTE q;
ENDStep 3
Call procedure_new
Step 4select * from products
Regards,Sameer S
1
Answers
-
When I need to "unpivot" with Domo, I like to go with ETL. There is a tile called Collapse Columns that does what you are wanting to do:Configuration
This will "unpivot" the data for you... like so:
Preview
1 -
I am trying to use a Dynamic solution thru below query:
SELECT GROUP_CONCAT(
CONCAT(
'SELECT `Product Type`, ',
QUOTE(COLUMN_NAME), ' AS `Date`, ',
'`', COLUMN_NAME, '` AS `Revenue` ',
'FROM testing_unpivot'
) SEPARATOR ' UNION ALL '
)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'testing_unpivot'
AND COLUMN_NAME <> 'Product Type';It doesn't allow me to generate output. It gives an error:
The database reported a syntax error. Incorrect column name 'GROUP_CONCAT( CONCAT( 'SELECT `Product Type`, ',<br> QUOTE(COLUMN_NAME), ' AS `Date`, ', 'Can you point the error here? I sincerely appreciate your response.
0 -
I got this one finally. Below is the sql code
Step 1
SELECT GROUP_CONCAT(
CONCAT(
' SELECT `Product Type` , ' ,QUOTE(COLUMN_NAME) , ' AS `Date`, ','`', COLUMN_NAME , '` AS `Revenue` ',
'FROM testing_unpivot'
) SEPARATOR ' UNION ALL '
) as product_info
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'testing_unpivot'
AND COLUMN_NAME <> 'Revenue Type';Step 2
CREATE PROCEDURE procedure_new()
BEGIN
SELECT product_info into @sql2 from transform_data_1;
SET @str=concat('create table products as ',@sql2);
PREPARE q from @str;
EXECUTE q;
ENDStep 3
Call procedure_new
Step 4select * from products
Regards,Sameer S
1 -
Excellent work!
I do have a few suggestions though...
The first is just a typo, you'll want to change the last line of Step 1 to "AND COLUMN_NAME <> 'Product Type';"
I would then change Step 4 because as it stands, the Date field is a VARCHAR and not formatted as a date.
SELECT
`Product Type`
,str_to_date(REPLACE(`Date`,'/',' '), '%m %d %Y') as `Date`
,`Revenue`FROM products
ORDER by `Product Type`, `Date`0 -
Thanks Scott for the review. I will amend the step 4 as yours! ?
Regards,
Sameer S
0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.6K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 102 SQL DataFlows
- 626 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 755 Beast Mode
- 61 App Studio
- 41 Variables
- 693 Automate
- 178 Apps
- 456 APIs & Domo Developer
- 49 Workflows
- 10 DomoAI
- 38 Predict
- 16 Jupyter Workspaces
- 22 R & Python Tiles
- 398 Distribute
- 115 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 130 Manage
- 127 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 11 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 110 Community Announcements
- 4.8K Archive