简体   繁体   中英

Snowflake - how to split a single field (VARIANT) into multiple columns

Within Snowflake how could I split a string of data that's in a single field into multiple columns? This field is in a View and a type of Variant.

My View that I'm querying has 12 actual columns. 1 of those columns is a large string that I'd like to split into columns. One of the columns in the table is a key, so I'm trying to get the 6 attributes in the 1 column shown below into their own column. The string is made of column name and then value as you can see in the pattern. The data in the large string presents as shown below (changed actual names for corporate privacy reasons). I tried using SPLIT, but that didn't work as I needed. I appreciate the help.

FIELD NAME IN VIEW: MASTER_FIELD_PLAN_TYP

STRING IN VIEW'S FIELD:

{   "FIELD1_CD": "M",   "FIELD1_CD_DESCR": "MEDICAL",   "FIELD2_SRC_CD": "M",   "FIELD2_SRC_CD_DESCR": "MEDICAL",   "FIELD3_CD":  "M","FIELD3_CD_DESCR": "MEDICAL",   "FIELD4_SRC_CD": "M",   "FIELD4_SRC_CD_DESCR": "MEDICAL",   "FIELD5_CD": "MN",   "FIELD5_CD_DESCR": "MINNESOTA",   "FIELD6_ST_SRC_CD": "MN",   "FIELD6_ST_SRC_CD_DESCR": "MINNESOTA"   }

My attempt to make it look like a table in this area:

KEY MASTER_FIELD_PLAN_TYP
58,502,601 { "FIELD1_CD": "M1", "FIELD1_CD_DESCR": "MEDICAL1", "FIELD2_SRC_CD": "M2", "FIELD2_SRC_CD_DESCR": "MEDICAL2", "FIELD3_CD": "M3","FIELD3_CD_DESCR": "MEDICAL3", "FIELD4_SRC_CD": "M4", "FIELD4_SRC_CD_DESCR": "MEDICAL4", "FIELD5_CD": "MN5", "FIELD5_CD_DESCR": "STATE5", "FIELD6_ST_SRC_CD": "MN6", "FIELD6_ST_SRC_CD_DESCR": "STATE6" }

I tried using SPLIT, but it created rows. I'd like to have in columns if possible.

EDIT... I added verbiage about the field being a type of Variant.

Since that's stored as a string (per the question title) you can use PARSE_JSON to turn it into a variant and refer to the field names:

with X as
(
select parse_json($$
{
    "FIELD1_CD": "M1",
    "FIELD1_CD_DESCR": "MEDICAL1",
    "FIELD2_SRC_CD": "M2",
    "FIELD2_SRC_CD_DESCR": "MEDICAL2",
    "FIELD3_CD": "M3",
    "FIELD3_CD_DESCR": "MEDICAL3",
    "FIELD4_SRC_CD": "M4",
    "FIELD4_SRC_CD_DESCR": "MEDICAL4",
    "FIELD5_CD": "MN5",
    "FIELD5_CD_DESCR": "STATE5",
    "FIELD6_ST_SRC_CD": "MN6",
    "FIELD6_ST_SRC_CD_DESCR": "STATE6"
}
                  $$) as JSON
)
select  JSON:FIELD1_CD::string       as FIELD1_CD
       ,JSON:FIELD1_CD_DESCR::string as FIELD2_DESCR
from X

If that column is defined as a string, you should consider changing the column type to variant. This will internally columnarize the JSON fields for better performance, and there will be no parsing required using the PARSE_JSON function.

Edit: From the question update, since it's already a variant you can refer to it as shown in the final select statement here:

create or replace table T1(V variant);

insert into T1
select parse_json($$
{
    "FIELD1_CD": "M1",
    "FIELD1_CD_DESCR": "MEDICAL1",
    "FIELD2_SRC_CD": "M2",
    "FIELD2_SRC_CD_DESCR": "MEDICAL2",
    "FIELD3_CD": "M3",
    "FIELD3_CD_DESCR": "MEDICAL3",
    "FIELD4_SRC_CD": "M4",
    "FIELD4_SRC_CD_DESCR": "MEDICAL4",
    "FIELD5_CD": "MN5",
    "FIELD5_CD_DESCR": "STATE5",
    "FIELD6_ST_SRC_CD": "MN6",
    "FIELD6_ST_SRC_CD_DESCR": "STATE6"
}
  $$) as JSON
;

select   V:FIELD1_CD::string       as FIELD1_CD
        ,V:FIELD1_CD_DESCR::string as FIELD1_CD_DESCR
from T1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM