Dynamically Uncollapse Columns for Tabular Output
Hi,
I'm trying to create a stored procedure like in the link below and while its not erroring out, its also not uncollapsing columns like it says it would. I'm not great at SQL and I've tried this in ETL but I can't get it to work there either.
https://knowledge.domo.com/@api/deki/files/5209/Dynamic_Pivot.sql?revision=1
What I'm trying to do is to take a dataset that looks like this and have 2 different types of cards (a multi-line chart with the EBProjectHealth as the series and a table that looks like what's below)
Input:
Output 1:
One row for each ProjectName
Output 2:
Count of Distinct ProjectName for each month, by EBProjectHealth (as scope)
When I run the stored procedure, it gives me an output that looks like the original input above with back ticks surrounding the date field. I also tried it on the ProjectName field with the same result.
Thoughts?
Best Answer
-
I see why.
Your Date field is Text format, not a date. Can you convert it to a Date earlier in the data flow? then this should work.
“There is a superhero in all of us, we just need the courage to put on the cape.” -Superman0
Answers
-
Try creating these two transorms in your data flow:
number 1:
CREATE PROCEDURE pivot()
BEGIN
set @sql_dynamic = (
SELECT
group_concat( DISTINCT
concat(
'max(case when month(Date)='
,month(`Date`)
,' and year(Date)='
,year(`Date`)
,' then EBProjectHealth end) as `'
,monthname(`Date`)
,' '
,year(`Date`)
,'`'
)
) as `test`
FROM `dojo_help_jlazerus`);
SET @sql = concat('create table pivot as ','Select ProjectName, ',
@sql_dynamic, '
FROM dojo_help_jlazerus
Group by ProjectName WITH ROLLUP'
);
PREPARE stmt from @sql;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;END
number 2:
CALL pivot()
this will create a table named pivot that will pivot all of your months.
“There is a superhero in all of us, we just need the courage to put on the cape.” -Superman0 -
Thanks for the reply. When I run this, I get this error on running the Call pivot()
The database reported a syntax error. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1
0 -
Did I get the field names correct?
I used "ProjectName", "Date", and "EBProjectHealth"
“There is a superhero in all of us, we just need the courage to put on the cape.” -Superman0 -
Yes. Those look correct.
0 -
That's odd. It works for me. Does it error out when you run the Create procedure transform too?
“There is a superhero in all of us, we just need the courage to put on the cape.” -Superman0 -
It does not error when I create the procedure.
1 -
Maybe I posted an old version of the code. Try copying this in again?
CREATE PROCEDURE pivot()
BEGIN
set @sql_dynamic = (
SELECT
group_concat( DISTINCT
concat(
'max(case when month(Date)='
,month(`Date`)
,' and year(Date)='
,year(`Date`)
,' then EBProjectHealth end) as `'
,monthname(`Date`)
,' '
,year(`Date`)
,'`'
)
) as `test`
FROM `dojo_help_jlazerus`);
SET @sql = concat('create table pivot as ','Select ProjectName, ',
@sql_dynamic, '
FROM dojo_help_jlazerus
Group by ProjectName WITH ROLLUP'
);
PREPARE stmt from @sql;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;END
“There is a superhero in all of us, we just need the courage to put on the cape.” -Superman1 -
Same error. And just to validate, the only thing I'm changing on your script is the FROM table in the SELECT and SET sections, right?
1 -
“There is a superhero in all of us, we just need the courage to put on the cape.” -Superman1 -
Actually, you can probably drop the "WITH ROLLUP" text as well
“There is a superhero in all of us, we just need the courage to put on the cape.” -Superman0 -
Those are the fields i'm changing and I also tried without the WITH ROLLUP text and am getting the same error.
0 -
I see why.
Your Date field is Text format, not a date. Can you convert it to a Date earlier in the data flow? then this should work.
“There is a superhero in all of us, we just need the courage to put on the cape.” -Superman0 -
Good catch. That fixed that error. This is amazing! Thank you so much.
1
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 296 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 614 Datasets
- 2.2K Magic ETL
- 3.8K Visualize
- 2.5K Charting
- 729 Beast Mode
- 53 App Studio
- 40 Variables
- 677 Automate
- 173 Apps
- 451 APIs & Domo Developer
- 45 Workflows
- 8 DomoAI
- 34 Predict
- 14 Jupyter Workspaces
- 20 R & Python Tiles
- 394 Distribute
- 113 Domo Everywhere
- 275 Scheduled Reports
- 6 Software Integrations
- 121 Manage
- 118 Governance & Security
- Domo Community Gallery
- 32 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 108 Community Announcements
- 4.8K Archive