Joining One-to-Many relationship datasets
I have two datasets that I am trying to join together, posts and metadata. I am figuring out how to do this via a pivot.
The data schema looks like the following:
# post schema
ID | Name | Type
1 | Post 1 | post
2 | Post 2 | post
# metadata schema
Object UID | Field Name | Field Value
post:1 | Number of Cats | 2
post:1 | Number of Dogs | 0
# schema I'm trying to create
# post-metadata
ID | Name | Type | Field Name 1 | Field Value 1 | Field Name 2 | Field Value 2
1 | Post 1 | post | Number of Cats | 2 | Number of Dogs | 0
2 | Post 2 | post | NULL | NULL | NULL | NULL
Would anyone have an idea of how to create this via a DOMO ETL or some other mechanism?
Comments
-
First you need a clean id to join on. I don't know if those are typos under your [Object UID] field or not, but it would need to have all non-numeric characters removed. Then you can join the two tables normally. Then you have the lift of creating a new "Number of Cats" field and one for dogs, etc...
- For the clean id you need a numeric column in your metadata table that has the matching id from the post schema. I would use regular expressions with a "Replace Text" action: https://knowledge.domo.com/Prepare/Magic_Transforms/ETL_DataFlows/04ETL_Actions%3A_Edit_Data#Replace_Text
- Choose the Object UID column
- Type \d in the box. Then click the 'gear' and select "Use Regex."
- Leave this box blank.
- You can do that in a magic etl with "Uncollapse Columns": https://knowledge.domo.com/Prepare/Magic_Transforms/ETL_DataFlows/02ETL_Actions%3A_Edit_Columns#Uncollapse_Columns which isn't the easiest action to use, but if you follow the documentation and play around with the settings you can get it to do what you are looking for.
**Say "Thanks" by clicking the "heart" in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"1 - For the clean id you need a numeric column in your metadata table that has the matching id from the post schema. I would use regular expressions with a "Replace Text" action: https://knowledge.domo.com/Prepare/Magic_Transforms/ETL_DataFlows/04ETL_Actions%3A_Edit_Data#Replace_Text
-
It's a formatting issue with the code block option on DOMO's form. I'll see if I can update the code block. The data in the dataset is clean ("Object UID").
0 -
@n8isjackdo you know if there's a way to dynamically generate the pivot columns based on the field names? I have a LOT of field names in our dataset, and I don't think it'll be feasible to manually create a column name for each field name in the "Uncollapse Columns" option.
I think the other option I might be able to use is potentially use prepared SQL statement with DOMO's SQL transform option.0 -
I would start by pivoting your metadata schema
Rank the field Names for each Object UID:
select
`Object UID`
,`Field Name`
,`Field Value`
,@rank := CASE
WHEN @UID = `Object UID` AND @rankval = `Field Name` then @rank
WHEN @UID = `Object UID` AND (@rankval := `Field Name`) IS NOT NULL then @rank+1
WHEN (@UID := `Object UID`) IS NOT NULL AND (@rankval := `Field Name`) IS NOT NULL then 1
END AS `Object UID Rank`
FROM `metadata_table`This will rank the number of different field name's you have for each object UID.
Now you can pivot this table
select
`Object UID`
,case when `Object UID Rank` = 1 then `Field Name` end as `Field Name 1`
,case when `Object UID Rank` = 1 then `Field Value` end as `Field Value 1`
,case when `Object UID Rank` = 2 then `Field Name` end as `Field Name 2`
,case when `Object UID Rank` = 2 then `Field Value` end as `Field Value 2`
,case when `Object UID Rank` = 3 then `Field Name` end as `Field Name 3`
,case when `Object UID Rank` = 3 then `Field Value` end as `Field Value 3`
from `metadata_rank_table`(you could use dynamic SQL to write the step above if needed)
Then you would need to join this table to your post schema table... presumably by using
concat(`Type`,':',`ID`) to join with `Object UID`
1 -
I used the method describe in https://knowledge.domo.com/Prepare/DataFlow_Tips_and_Tricks/Dynamic_Pivot
to pivot my data most of the time. It works well. Just need to be careful about the apostrophe and quote.
The script below correspond to each of the objects in the attached image "transform"
raw data
transform
#Transforms - Table : setup
select ID, CONCAT('`', ColumnName, '`') As ColumnName, ColumnValue from `row_data`
#Transform - SQL
CREATE PROCEDURE Pivot()
BEGINSET @cols =
(SELECT
GROUP_CONCAT(DISTINCT
CONCAT('MAX(IF(ColumnName = ''', ColumnName, ''', ColumnValue, NULL)) AS ', ColumnName)
) AS c
FROM setup);SET @sql = (SELECT CONCAT('CREATE TABLE Pivot AS SELECT ID , ', @cols, '
FROM setup
GROUP BY ID'));
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
#Transform - SQL
Call Pivot
#Output Datasets
select * from Pivotpivot result
0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 620 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 742 Beast Mode
- 58 App Studio
- 41 Variables
- 686 Automate
- 176 Apps
- 453 APIs & Domo Developer
- 47 Workflows
- 10 DomoAI
- 36 Predict
- 15 Jupyter Workspaces
- 21 R & Python Tiles
- 395 Distribute
- 113 Domo Everywhere
- 276 Scheduled Reports
- 6 Software Integrations
- 124 Manage
- 121 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 108 Community Announcements
- 4.8K Archive