<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic UNPIVOT VARIANT data in SQL in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/unpivot-variant-data-in-sql/m-p/100554#M40331</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;Have a VARIANT column with the following data;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;CREATE TABLE unpivot_valtype AS
SELECT parse_json(
'{
  "Id": 1234567,
  "Result": {
      "BodyType": "NG",
      "ProdType": "Auto",
      "ResultSets": [
        {
        "R1": {
          "AInt": 1,
          "CRates": 
            {
              "RateA": 11,
              "RateB": 12,
              "RateC": 13
            }
          ,
          "EffPrice": 14,
          "EffRate": 15
        },
        "R2": {
          "AInt": 2,
          "CRates": 
            {
              "RateA": 21,
              "RateB": 22,
              "RateC": 23
            }
          ,
          "EffPrice": 24,
          "EffRate": 25
        },
        "R3": {
          "AInt": 3,
          "CRates": 
            {
              "RateA": 31,
              "RateB":32,
              "RateC": 33
            }
          ,
          "EffPrice": 34,
          "EffRate": 35
        } } ]
    }
}') as rowData&lt;/LI-CODE&gt;&lt;P&gt;and I want to convert "&lt;STRONG&gt;ResultSets&lt;/STRONG&gt;" to rows as seen below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="result.png" style="width: 590px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/13259iDBD2710CB5DFA4D4/image-size/large?v=v2&amp;amp;px=999" role="button" title="result.png" alt="result.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Not sure how I should flatten it this way.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Burhan&lt;/P&gt;</description>
    <pubDate>Mon, 02 Dec 2024 06:06:26 GMT</pubDate>
    <dc:creator>binsel</dc:creator>
    <dc:date>2024-12-02T06:06:26Z</dc:date>
    <item>
      <title>UNPIVOT VARIANT data in SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/unpivot-variant-data-in-sql/m-p/100554#M40331</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;Have a VARIANT column with the following data;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;CREATE TABLE unpivot_valtype AS
SELECT parse_json(
'{
  "Id": 1234567,
  "Result": {
      "BodyType": "NG",
      "ProdType": "Auto",
      "ResultSets": [
        {
        "R1": {
          "AInt": 1,
          "CRates": 
            {
              "RateA": 11,
              "RateB": 12,
              "RateC": 13
            }
          ,
          "EffPrice": 14,
          "EffRate": 15
        },
        "R2": {
          "AInt": 2,
          "CRates": 
            {
              "RateA": 21,
              "RateB": 22,
              "RateC": 23
            }
          ,
          "EffPrice": 24,
          "EffRate": 25
        },
        "R3": {
          "AInt": 3,
          "CRates": 
            {
              "RateA": 31,
              "RateB":32,
              "RateC": 33
            }
          ,
          "EffPrice": 34,
          "EffRate": 35
        } } ]
    }
}') as rowData&lt;/LI-CODE&gt;&lt;P&gt;and I want to convert "&lt;STRONG&gt;ResultSets&lt;/STRONG&gt;" to rows as seen below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="result.png" style="width: 590px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/13259iDBD2710CB5DFA4D4/image-size/large?v=v2&amp;amp;px=999" role="button" title="result.png" alt="result.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Not sure how I should flatten it this way.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Burhan&lt;/P&gt;</description>
      <pubDate>Mon, 02 Dec 2024 06:06:26 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unpivot-variant-data-in-sql/m-p/100554#M40331</guid>
      <dc:creator>binsel</dc:creator>
      <dc:date>2024-12-02T06:06:26Z</dc:date>
    </item>
    <item>
      <title>Re: UNPIVOT VARIANT data in SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/unpivot-variant-data-in-sql/m-p/100566#M40333</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/105610"&gt;@binsel&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;You need to use variant_explode function.&lt;BR /&gt;Here is the working code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;WITH first_explode AS (
  SELECT  
    uv.rowData:Id AS Id,
    uv.rowData:Result:BodyType AS BodyType,
    uv.rowData:Result:ProdType AS ProdType,
    v.value AS result_set
  FROM unpivot_valtype AS uv,
  LATERAL variant_explode(uv.rowData:Result:ResultSets) AS v
),
second_explode AS (
  SELECT
    fe.Id,
    fe.BodyType,
    fe.ProdType,
    vv.key AS R,
    vv.value AS r_value
  FROM first_explode fe,
  LATERAL variant_explode(fe.result_set) AS vv
)
SELECT
  Id,
  BodyType,
  ProdType,
  R,
  r_value:AInt AS AInt,
  r_value:CRates:RateA AS RateA,
  r_value:CRates:RateB AS RateB,
  r_value:CRates:RateC AS RateC,
  r_value:EffPrice AS EffPrice,
  r_value:EffRate AS EffRate
FROM
  second_explode;&lt;/LI-CODE&gt;&lt;P&gt;The result:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="filipniziol_0-1733129113052.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/13265iA22D3E05C7CA5957/image-size/medium?v=v2&amp;amp;px=400" role="button" title="filipniziol_0-1733129113052.png" alt="filipniziol_0-1733129113052.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Dec 2024 08:45:27 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unpivot-variant-data-in-sql/m-p/100566#M40333</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-12-02T08:45:27Z</dc:date>
    </item>
    <item>
      <title>Re: UNPIVOT VARIANT data in SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/unpivot-variant-data-in-sql/m-p/100656#M40371</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/117376"&gt;@filipniziol&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realized &lt;STRONG&gt;CRates&lt;/STRONG&gt; are &lt;STRONG&gt;arrays&lt;/STRONG&gt;. I guess I have to use another function and add one more layer, right?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;CREATE TABLE unpivot_valtype AS
SELECT parse_json(
'{
    "Id": 1234567,
  "Result": {
      "BodyType": "NG",
      "ProdType": "Auto",
      "ResultSets": [
        {
        "R1": {
          "AInt": 1,
          "CRates": [
            {
              "RateA": 11,
              "RateB": 12,
              "RateC": 13
            },
            {
              "RateA": 111,
              "RateB": 112,
              "RateC": 113
            }]
          ,
          "EffPrice": 14,
          "EffRate": 15
        },
        "R2": {
          "AInt": 2,
          "CRates": [
            {
              "RateA": 21,
              "RateB": 22,
              "RateC": 23
            },
            {
              "RateA": 221,
              "RateB": 222,
              "RateC": 223
            }]
          ,
          "EffPrice": 24,
          "EffRate": 25
        },
        "R3": {
          "AInt": 3,
          "CRates": [
            {
              "RateA": 31,
              "RateB": 32,
              "RateC": 33
            },
            {
              "RateA": 331,
              "RateB": 332,
              "RateC": 333
            }]
          ,
          "EffPrice": 34,
          "EffRate": 35
        } } ]
    }
}') as rowData&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Dec 2024 17:56:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unpivot-variant-data-in-sql/m-p/100656#M40371</guid>
      <dc:creator>binsel</dc:creator>
      <dc:date>2024-12-02T17:56:34Z</dc:date>
    </item>
    <item>
      <title>Re: UNPIVOT VARIANT data in SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/unpivot-variant-data-in-sql/m-p/100708#M40385</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/105610"&gt;@binsel&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Yes, you need to unpack the CRates array as well&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;WITH first_explode AS (
  SELECT  
    uv.rowData:Id AS Id,
    uv.rowData:Result:BodyType AS BodyType,
    uv.rowData:Result:ProdType AS ProdType,
    v.value AS result_set
  FROM unpivot_valtype AS uv,
  LATERAL variant_explode(uv.rowData:Result:ResultSets) AS v
),
second_explode AS (
  SELECT
    fe.Id,
    fe.BodyType,
    fe.ProdType,
    vv.key AS R,
    vv.value AS r_value
  FROM first_explode fe,
  LATERAL variant_explode(fe.result_set) AS vv
),
third_explode AS (
  SELECT
    se.Id,
    se.BodyType,
    se.ProdType,
    se.R,
    se.r_value:AInt AS AInt,
    se.r_value:EffPrice AS EffPrice,
    se.r_value:EffRate AS EffRate,
    c.value AS c_rate
  FROM second_explode se,
  LATERAL variant_explode(se.r_value:CRates) AS c
)
SELECT
  Id,
  BodyType,
  ProdType,
  R,
  AInt,
  c_rate:RateA AS RateA,
  c_rate:RateB AS RateB,
  c_rate:RateC AS RateC,
  EffPrice,
  EffRate
FROM
  third_explode;&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="filipniziol_0-1733211624644.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/13287i220B530C28BF9124/image-size/medium?v=v2&amp;amp;px=400" role="button" title="filipniziol_0-1733211624644.png" alt="filipniziol_0-1733211624644.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Dec 2024 07:40:39 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unpivot-variant-data-in-sql/m-p/100708#M40385</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-12-03T07:40:39Z</dc:date>
    </item>
  </channel>
</rss>

