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

  • n8isjack
    n8isjack Contributor

    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...

     

    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
      1. Choose the Object UID column
      2. Type \d in the box. Then click the 'gear' and select "Use Regex."
      3. Leave this box blank.
    2. 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"
  • 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").

  • @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.

  • ST_Superman
    ST_Superman Domo Employee

    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`

  • 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 dataraw data

     

     

    transformtransform

    #Transforms - Table : setup
    select ID, CONCAT('`', ColumnName, '`') As ColumnName, ColumnValue from `row_data`


    #Transform - SQL
    CREATE PROCEDURE Pivot()
    BEGIN

    SET @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 Pivot

     

    pivot resultpivot result