Skip to content
Advertisement

Compare JSON Values and identify the differences -Snowflake SQL

I am trying to compare the set of two JSON values for every transaction and extract specific values from the below.. I want to extract the following values cCode,dCode,hcps and mod..Can you please guide me on the snowflake SQL syntax for the same.. The first JSON is done by coder and the second json by auditor

And I am looking for a result as the below:

Can anyone please help me here

sql Tried

This gives an error straightaway for using :

I have tried the code with the SQL server version but I need to know how to map the JSON functions to the Snowflake SQL version..Can you please help here..

( select , dense_rank() over (order by Billid, Ctextid) as tid, dense_rank() over (partition by Billid, Ctextid order by Created) as n from ##input1 ), D as ( select I., mk.[key] as mk, m., dk.[key] as dk, d. from I cross apply openjson(info) mk cross apply openjson(mk.value) with ( cCode nvarchar(max) ‘$.cCode’, dgoses nvarchar(max) ‘$.dgoses’ as json ) m cross apply openjson(dgoses) dk cross apply openjson(dk.value) with ( dCode nvarchar(max) ‘$.dCode’ ) d ), C as ( select * from D where n = 1 ), A as ( select * from D where n = 2 ) select Billid, codedby, Ctextid,

from I as t where n = 1

Thanks, Arun

Advertisement

Answer

I’m not entirely sure how you need the data, but you’re trying to get “cCode, dCode, hcps and mod” (assuming hcps is actually hcpc). The problem is cCode, hcpc, and mod are all on the same level of the JSON. dCode is not. It’s nested one layer down from the other properties and is a one to many relationship. This could be flattened out to two tables with a 1:MANY relationship, or it could be flattened out in a single table repeating the cCode, hcpc, and mod values. This example shows the second option:

This creates a table like this:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement