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
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"] |
+------------------------------------+
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"}] |
+----------+--------+----------------------------------------------------+--+
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))