SNOWFLAKE JSON with keys and values - snowflake-cloud-data-platform

I have a json in snowflake like that:
{"53CefwEQlSZyZmk4n9s24qGV3m":40,"rSwfCfEH01TrDCs9E0C3CL1fhi":107,"system":2}
And I need to transform it in separated columns like that:
user number
53CefwEQlSZyZmk4n9s24qGV3m 40
system 2
rSwfCfEH01TrDCs9E0C3CL1fhi 107
Can you help me please? thanks!

Flatten it out and treat it as key/value pairs:
with JSON as
(
select parse_json('{"53CefwEQlSZyZmk4n9s24qGV3m":40,"rSwfCfEH01TrDCs9E0C3CL1fhi":107,"system":2}') as V
)
select KEY as USER
,VALUE as NUMBER
from JSON, table(flatten(V));
USER
NUMBER
53CefwEQlSZyZmk4n9s24qGV3m
40
rSwfCfEH01TrDCs9E0C3CL1fhi
107
system
2

Related

Get rows from different table in a column in select query

I want to be able to see the rows from different table in a single column .
Just like this question How to select row/s from an other table using a comma-separated value from the first table? (which is a duplicate of this question: MySQL Join two tables with comma separated values :) but for Sql Server.
Tables from link:
Table 1. faculty
subject
101, 102
104
103, 105
Table 2. subject
code
subject
101
subject 1
102
subject 2
103
subject 3
104
subject 4
105
subject 5
Expected Output:
subject
subject
101, 102
subject 1, subject 2
104
subject 4
103, 105
subject 3, subject 5
Basically I don't want to see multiple columns for joins but all corresponding values in on column comma separated.
To split a delimited string into rows in newer versions of sql server you can use STRING_SPLIT() in a CROSS APPLY. Then after joining, to aggregate strings back into delimited form you can use function STRING_AGG. Putting the same pieces together as the linked question you will get the following. NOTE that this requires newer versions of sql server that support both STRING_AGG and STRING_SPLIT functionality.
With faculty_split AS
(
SELECT *
FROM faculty
CROSS APPLY STRING_SPLIT (subject, ',') fac
)
SELECT faculty_split.subject as codes, STRING_AGG(subject.subject, ',') as subjects
FROM faculty_split
INNER JOIN subject on faculty_split.value = subject.code
GROUP BY faculty_split.subject
Link to fiddle

BigQuery ARRAY_TO_STRING based on condition in non-array field

I have a table that I query like this...
select *
from table
where productId = 'abc123'
Which returns 2 rows (even though the productId is unique) because one of the columns (orderName) is an Array...
**productId, productName, created, featureCount, orderName**
abc123, someProductName, 2020-01-01, 12, someOrderName
, , , , someOtherOrderName
I'm not sure whether the missing values in the 2nd row are empty strings or nulls because of the way the orderName array expands my search results but I want to now run a query like this...
select productName, ARRAY_TO_STRING(orderName,'-')
from table
where productId = 'abc123'
and ifnull(featureCount,0) > 0
But this query returns...
someProductName, someOrderName-someOtherOrderName
i.e. both array values came back even though I specified a condition of featureCount>0.
I'm sure I'm missing something very basic about how Arrays function in BigQuery but from Google's ARRAY_TO_STRING documentation I don't see any way to add a condition to the extracting of ARRAY values. Appreciate any thoughts on the best way to go about this.
For what I understand, this is because you are just querying one row of data which have a column as ARRAY<STRING>. As you are using ARRAY_TO_STRINGS it will only accept ARRAY<STRING> values you will see all array values fit into just one cell.
So, when you run your script, your output will fit your criteria and return the columns with arrays with additional rows for visibility.
The visualization on the UI should look like your mention in your question:
Row
productId
productName
created
featureCount
orderName
1
abc123
someProductName
2020-01-01
12
someOrderName
someOtherOrderName
Note: On bigquery this additional row is gray out ( ) and Its part of row 1 but it shows as an additional row for visibility. So this output only have 1 row in the table.
And the visualization on a JSON will be:
[
{
"productId": "abc123",
"productName": "someProductName",
"created": "2020-01-01",
"featureCount": "12",
"orderName": [
"someOrderName",
"someOtherOrderName"
]
}
]
I don't think there is specific documentation info about how you visualize arrays on UI but I can share the docs that talks about how to flattening your rows outputs into a single row line, check:
Working with Arrays
Flattening Arrays
I use the following to replicate your issue:
CREATE OR REPLACE TABLE `project-id.dataset.working_table` (
productId STRING,
productName STRING,
created STRING,
featureCount STRING,
orderName ARRAY<STRING>
);
insert into `project-id.dataset.working_table` (productId,productName,created,featureCount,orderName)
values ('abc123','someProductName','2020-01-01','12',['someOrderName','someOtherOrderName']);
insert into `project-id.dataset.working_table` (productId,productName,created,featureCount,orderName)
values ('abc123X','someProductNameX','2020-01-02','15',['someOrderName','someOtherOrderName','someData']);
output
Row
productId
productName
created
featureCount
orderName
1
abc123
someProductName
2020-01-01
12
someOrderName
someOtherOrderName
2
abc123X
someProductNameX
2020-01-02
15
someOrderName
someOtherOrderName
someData
Note: Table contains 2 rows.

Use the result of a Select statement result for another query in mssql server 2008

I have a table forms from which I query out a set of result using
SELECT form_id from forms where customer_id = 200.
I get a set of results like
14
67
90
177
289
800
Now I have separate table based on these form id's like form_{form_id}details. I want to look into each of the table based on the form id's obtained and count the number of records. If form{form_id}_details has any record (i.e count(*) as cnt > 0), I want to get the count. I'm using PHP along with mssql server. Can anyone help me in this?

Get Filtered numeric data from alphanumeric field in binding Source control in winform application

I have a Binding Source control, which is binded to a table named Customer. There is a column named "CustomerNo", in this table which contains alphanumeric data. I want to apply a filter on Binding Source that it only shows rows which has only numeric values in "CustomerNo" and value must be in range 13 to 20.
for example if i have following data in CustomerNo column.
10
11
12
13
14
as55
as66
as77
15
so it should give me the following result.(As my filter range is 13-20 and i need only numerice values)
14
15
Mean while i just want to apply filter on numeric values of alphanumeric column.
Kindly help me resolve my problem.
select CustomerNo from (
select CustomerNo from customers
where ISNUMERIC(CustomerNo )=1 ) c
where c.CustomerNo between 13 and 20
Use this query or create a view and use it

sql query to get data for Spacetree (jit)

I have a table "BinaryTree" and it has 2 columns "CustomerID" and "ParentID".
Another table is "Customers" and it has columns ""CustomerID" and "Firstname".
I want to query data from these table and want to assign these data to Spacetree in Json format.
Please have a reference of below links :-
http://philogb.github.io/jit/static/v20/Jit/Examples/Spacetree/example1.code.html
http://philogb.github.io/jit/static/v20/Jit/Examples/Spacetree/example1.html
I want data something like below :-
Parentid CustomerID FirstName
1 34 Test1
1 64 Test2
1 46 Test3
34 45 Test4
34 102 Test5
64 22 Test6
46 54 Test7
So I can build json string and assign it to spacetree.
I would be good if it return data in order, means for a parentid it first return all its childrens.
and after that it returns childrens of these childerns one by one, so it is easy to build json string in proper format that spacetree wants.
Please let me know if further information needed.
Thanks in advance guys.
use inner join.
select BT.PARID,BT.CUSTID,CU.firstname
from BinaryTree BT INNER JOIN Customers CU on BT.CUSTID=CU.CUSTID
ORDER BY BT.PARID,BT.CUSTID
check the demo : http://sqlfiddle.com/#!3/1ffc1/1

Resources