Split string and output into rows - snowflake-cloud-data-platform

I want to split a string based on delimiter ',' and put the results into rows. Hence, I'm trying to use SPLIT_TO_TABLE function in Snowflake, but not working successfully.
I used the regexp_replace to clean the string. How can I output this into rows for each id?
SELECT value,
TRIM(regexp_replace(value, '[{}_]', ' ')) AS extracted
Here is the sample data:
+--------+------------------------------------+
| id | value |
+--------+------------------------------------+
| fsaf12 | {Other Questions,Missing Document} |
| sfas11 | {Other} |
+--------+------------------------------------+
Expected result:
+--------+------------------+
| id | extracted |
+--------+------------------+
| fsaf12 | Other Questions |
| fsaf12 | Missing Document |
| sfas11 | Others |
+--------+------------------+

Adding another way to split the data and output it as rows :
SELECT b,TRIM(regexp_replace(splitvalue, '[{}_]', '')) AS extracted from
(SELECT b, C.value::string AS splitvalue
FROM split,
LATERAL FLATTEN(input=>split(a, ',')) C);
where a and b are the columns in table "split" and data is as follows :
A
B
{First,Second}
row1
{Third,Fourth}
row2

HEre is answer , used replace function instead of regexp_replace
WITH DATATABLE(ID ,VALUEA ) AS
(
SELECT * FROM VALUES ('fsaf12','{Other Questions,Missing Document}'),('sfas11',' {Other} ')
)
SELECT ID, REPLACE(REPLACE(VALUE,'{',''),'}','') SPLITTED_VALUE FROM DATATABLE , LATERAL SPLIT_TO_TABLE (VALUEA,',') ;

Related

Create 1 array with 2 fields from 2 csv fields in BigQuery

I am currently trying to work through this and I'm unsure as to how to proceed. I have the below data
ID
name
value
One
a,b,c
10,20,30
I would like to turn it into
| ID | properties.name | properties.value |
|:---- |:------: | -----: |
| One | a | 10 |
| | b | 20 |
| | c | 30 |
The below query looked like it was working but instead of having an array it created a nested record with 2 array fields.
SELECT ID
name
, value
, array (
select as struct
split(name, ',') as name
, split(value, ',') as value
) as properties
FROM `orders`
Consider below approach
select id, array(
select as struct name, value
from unnest(split(name)) name with offset
join unnest(split(value)) value with offset
using(offset)
) as properties
from `orders`
if applied to sample data in your question - output is

Accessing JSON Array of strings

My problem is that I have a JSON column with a key which contains an array of string values. If I access it with array notation:
-- JSON Key = "infoProvided":["FullName","Contact Information"]
select top 1 json_value(textarea,'$.infoProvided[0]') from checklist
-- result
(No column name)
FullName
-- expected
(No column name)
FullName,Contact Information
I need to get all possible values in the array. Thanks in advance.
Note: I had to edit your JSON as it was missing the open and closing curly braces.
DECLARE #json VARCHAR(1000) = '{"infoProvided":["FullName","Contact Information"]}';
SELECT * FROM OPENJSON ( #json, '$.infoProvided' );
Returns
+-----+---------------------+------+
| key | value | type |
+-----+---------------------+------+
| 0 | FullName | 1 |
| 1 | Contact Information | 1 |
+-----+---------------------+------+
SELECT STRING_AGG ( [value], ',' ) AS InfoList FROM OPENJSON ( #json, '$.infoProvided' );
Returns
+------------------------------+
| InfoList |
+------------------------------+
| FullName,Contact Information |
+------------------------------+
SELECT * FROM OPENJSON ( #json, '$' );
Returns
+--------------+------------------------------------+------+
| key | value | type |
+--------------+------------------------------------+------+
| infoProvided | ["FullName","Contact Information"] | 4 |
+--------------+------------------------------------+------+
SELECT JSON_QUERY ( #json, '$.infoProvided' );
Returns
+------------------------------------+
| (No column name) |
+------------------------------------+
| ["FullName","Contact Information"] |
+------------------------------------+

how to convert string into complex array of struct and explode in hive

I have below hive table
id string
code string
config string
values:
dummyID|codeA|[{"pmc":"111","scc":"aa1","pgtp":"a22","pgn":"a33","pgrc":"a44"},{"pmc":"222","scc":"bb1","pgtp":"b22","pgn":"b33","pgrc":"b44","sen":"b77"},{"pmc":"333","scc":"cc1","pgtp":"c22","pgn":"c33","pgrc":"c44","pscc":[],"mapb":"c88"},{"pmc":"444","scc":"dd1","pgtp":"d22","pgn":"d33","pgrc":"d44","pscc":["ghgh"],"mapb":"d88"},{"pmc":"555","scc":"ee1","pgtp":"e22","pgn":"e33","pgrc":"e44","mapb":"e88"}]
I need to explode the array like below output : ( any of the elements under struct can be optional)
dummyID|codeA|{"pmc":"111","scc":"aa1","pgtp":"a22","pgn":"a33","pgrc":"a44"}
dummyID|codeA|{"pmc":"222","scc":"bb1","pgtp":"b22","pgn":"b33","pgrc":"b44","sen":"b77"}
dummyID|codeA|{"pmc":"333","scc":"cc1","pgtp":"c22","pgn":"c33","pgrc":"c44","pscc":[{"qtgm":"tt1","swrt":"rr2"}],"mapb":"c88"}
dummyID|codeA|{"pmc":"444","scc":"dd1","pgtp":"d22","pgn":"d33","pgrc":"d44","pscc":["ghgh"],"mapb":"d88"}
dummyID|codeA|{"pmc":"555","scc":"ee1","pgtp":"e22","pgn":"e33","pgrc":"e44","mapb":"e88"}
I tried:
select
id,
code,
exp_val
FROM temp
LATERAL VIEW explode(array(config)) temp AS exp_val ;
above query is not giving any error but not exploding and getting single row,
lateral view inline didn't work too
I tried to create table with below schema and tried to insert records from above string config field but it failed with data type mismatch error
id string,
code string,
config array<struct<pmc:String,scc:String,pgtp:string,pgn:string,pgrc:string,pscc:Array<String>,sen:Array<String>,mapb:Array<String>>>
when I tried to run select query for config i got below result
|dummyID|codeA|{"pmc":"[{\"pmc\":\"111\",\"scc\":\"aa1\",\"pgtp\":\"a22\",\"pgn\":\"a33\",\"pgrc\":\"a44\"},{\"pmc\":\"222\",\"scc\":\"bb1\",\"pgtp\":\"b22\",\"pgn\":\"b33\",\"pgrc\":\"b44\",\"sen\":\"b77\"},{\"pmc\":\"333\",\"scc\":\"cc1\",\"pgtp\":\"c22\",\"pgn\":\"c33\",\"pgrc\":\"c44\",\"pscc\":[],\"mapb\":\"c88\"},{\"pmc\":\"444\",\"scc\":\"dd1\",\"pgtp\":\"d22\",\"pgn\":\"d33\",\"pgrc\":\"d44\",\"pscc\":[\"ghgh\"],\"mapb\":\"d88\"},{\"pmc\":\"555\",\"scc\":\"ee1\",\"pgtp\":\"e22\",\"pgn\":\"e33\",\"pgrc\":\"e44\",\"mapb\":\"e88\"}]","scc":null,"pgtp":null,"pgn":null,"pgrc":null,"pscc":null,"sen":null,"mapb":null}
Explode did'nt work on this dataset too
Is there anything I am missing?
remove array inside explode function and try the following
select
id,
code,
exp_val
FROM temp
LATERAL VIEW explode(config) temp AS exp_val ;
Second Option:
select
t.id,
t.code,
e.*
FROM temp t
LATERAL VIEW outer inline(t.config) e ;
You can try this approach,
First SPLIT the String to transform it in Array of Type String.
Second Explode the array.
As an example
WITH table AS(
select 'dummyID' AS id,'codeA' AS code, SPLIT('[{"pmc":"111","scc":"aa1","pgtp":"a22","pgn":"a33","pgrc":"a44"},{"pmc":"222","scc":"bb1","pgtp":"b22","pgn":"b33","pgrc":"b44","sen":"b77"},{"pmc":"333","scc":"cc1","pgtp":"c22","pgn":"c33","pgrc":"c44","pscc":[],"mapb":"c88"},{"pmc":"444","scc":"dd1","pgtp":"d22","pgn":"d33","pgrc":"d44","pscc":["ghgh"],"mapb":"d88"},{"pmc":"555","scc":"ee1","pgtp":"e22","pgn":"e33","pgrc":"e44","mapb":"e88"}]','\\{') AS array)
SELECT id,code, exp_val
FROM table
LATERAL VIEW explode(array) table AS exp_val;
ouput
+----------+--------+----------------------------------------------------+--+
| id | code | exp_val |
+----------+--------+----------------------------------------------------+--+
| dummyID | codeA | [ |
| dummyID | codeA | "pmc":"111","scc":"aa1","pgtp":"a22","pgn":"a33","pgrc":"a44"}, |
| dummyID | codeA | "pmc":"222","scc":"bb1","pgtp":"b22","pgn":"b33","pgrc":"b44","sen":"b77"}, |
| dummyID | codeA | "pmc":"333","scc":"cc1","pgtp":"c22","pgn":"c33","pgrc":"c44","pscc":[],"mapb":"c88"}, |
| dummyID | codeA | "pmc":"444","scc":"dd1","pgtp":"d22","pgn":"d33","pgrc":"d44","pscc":["ghgh"],"mapb":"d88"}, |
| dummyID | codeA | "pmc":"555","scc":"ee1","pgtp":"e22","pgn":"e33","pgrc":"e44","mapb":"e88"}] |
+----------+--------+----------------------------------------------------+--+

Select unique rows with where in clause (SQL Server)

I am trying to return one json per unique value in a column.
However, when I try to select from a sub-query the entire result set is returned.
Example Table
| json | column |
|--------|--------|
| {obj1} | 1 |
| {obj2} | 1 |
| {obj3} | 2 |
Example Query
select distinct
[json]
from someTable
where [column] in (
select distinct
[column]
from someTable
)
Actual Output
| json |
|--------|
| {obj1} |
| {obj2} |
| {obj3} |
Expected Output
| json |
|--------|
| {obj1} |
| {obj3} |
How can I select just 1 json per unique column value?
If doesn't Matter output Related To Column is obj1 Or obj2 you must Use Group By Like this :
SELECT
Min(JSon) AS Json ,Column
FROM
someTable
Group By column
If you want the 1st [json] of each column then use row_number():
select t.[json] from (
select
[json],
row_number() over (partition by column order by id) rn
from someTable
) t
where t.rn = 1

SQL select all ID's into temp table according to comma delimited string

I have a table structured as such:
| ID | Name |
| 1 | Bob |
| 2 | Jim |
| 3 | Jane |
. .
. .
. .
I am trying to compose a query that will return all ID's of the names that I will be passing. Note that the names will be passed as a comma delimited string.
The query i've tried is:
#Names = 'Bob, Jane'
select ID into #Ids from Users where Name in ((select i.Item from dbo.Split(#Names, ',', 0) as i))
What I was hoping to get was:
| ID |
| 1 |
| 3 |
but instead I just get:
| ID |
| 1 |
I would have to loop through this query, but what is the best way to do so? Am I approaching this problem correctly?
The problem might be the trailing spaces in the output of split function.
After comma you have a space. It should be removed because in SQL Server only = operator ignores trailing spaces when making the comparison. Use Ltrim and Rtrim functions to remove the trailing spaces before making comparison
SELECT ID
INTO #Ids
FROM Users
WHERE NAME IN ((SELECT rtrim(ltrim(i.Item))
FROM dbo.Split(#Names, ',', 0) AS i))

Resources