简体   繁体   English

比较 JSON 值并识别差异 -Snowflake SQL

[英]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我正在尝试比较每个事务的两个 JSON 值的集合,并从下面提取特定值.. 我想提取以下值 cCode、dCode、hcps 和 mod..你能指导我了解雪花 SQL 语法吗同样.. 第一个 JSON 由编码员完成,第二个 json 由审计员完成

1st json
[
  {
    "cCode": "7832",
    "Date": "08/26/2020",
    "ID": "511",
    "description": "holos",
    "dgoses": [
      {
        "description": "disease",
        "dCode": "Y564",
        "CodeAllId": "8921",
        "messages": [
          ""
        ],
        "sequenceNumber": 1
      },
      {
        "description": "acute pain",
        "dCode": "U3321",
        "CodeAllId": "33213",
        "messages": [
          ""
        ],
        "sequenceNumber": 2
      },
      {
        "description": "height",
        "dCode": "U1111",
        "CodeAllId": "33278",
        "messages": [
          ""
        ],
        "sequenceNumber": 3
      },
      {
        "description": "PIDEMIA ",
        "dCode": "H8811",
        "CodeAllId": "90000",
        "messages": [
          ""
        ],
        "sequenceNumber": 4
      }
    ],
    "familyPlan": "",
    "hcpc": 5,
    "id": "",
    "isEPS": false,
    "mod": "67",
    "originalUnitAmount": "8888",
    "type": "CHARGE",
    "unitAmount": "9000",
    "vId": "90001"
  },
  {
    "cCode": "900114",
    "Date": "08/26/2020",
    "ID": "523",
    "description": "heart valve",
    "dgoses": [
      {
        "description": "Fever",
        "dCode": "J8923",
        "CodeAllId": "892138",
        "messages": [
          ""
        ],
        "sequenceNumber": 1
      }
    ],
    "familyPlan": "",
    "hcpc": 1,
    "id": "",
    "mod": "26",
    "originalUnitAmount": "19039",
    "type": "CHARGE",
    "unitAmount": "1039",
    "vId": "5113"
  }
]

2nd JSON
[
  {
    ""cCode"": ""78832"",
    ""Date"": ""08/26/2020"",
    ""ID"": ""511"",
    ""description"": ""holos"",
    ""dgoses"": [
      {
        ""description"": ""disease"",
        ""dCode"": ""Y564"",
        ""CodeAllId"": ""8921"",
        ""messages"": [
          """"
        ],
        ""sequenceNumber"": 1
      },
      {
        ""description"": ""acute pain"",
        ""dCode"": ""U3321"",
        ""CodeAllId"": ""33213"",
        ""messages"": [
          """"
        ],
        ""sequenceNumber"": 2
      },
      {
        ""description"": ""height"",
        ""dCode"": ""U41111"",
        ""CodeAllId"": ""33278"",
        ""messages"": [
          """"
        ],
        ""sequenceNumber"": 3
      },
      {
        ""description"": ""PIDEMIA "",
        ""dCode"": ""H8811"",
        ""CodeAllId"": ""90000"",
        ""messages"": [
          """"
        ],
        ""sequenceNumber"": 4
      }
    ],
    ""familyPlan"": """",
    ""hcpc"": 8,
    ""id"": """",
    ""isEPS"": false,
    ""mod"": ""67"",
    ""originalUnitAmount"": ""8888"",
    ""type"": ""CHARGE"",
    ""unitAmount"": ""9000"",
    ""vId"": ""90001""
  },
  {
    ""cCode"": ""900114"",
    ""Date"": ""08/26/2020"",
    ""ID"": ""523"",
    ""description"": ""heart valve"",
    ""dgoses"": [
      {
        ""description"": ""Fever"",
        ""dCode"": ""J8923"",
        ""CodeAllId"": ""892138"",
        ""messages"": [
          """"
        ],
        ""sequenceNumber"": 1
      }
    ],
    ""familyPlan"": """",
    ""hcpc"": 1,
    ""id"": """",
    ""mod"": ""126"",
    ""originalUnitAmount"": ""19039"",
    ""type"": ""CHARGE"",
    ""unitAmount"": ""1039"",
    ""vId"": ""5113""
  }
]

And I am looking for a result as the below:我正在寻找如下结果:

 Billid ctextid cCode-Coder cCode-Auditor   deletedccode    added-ccode dCode-Coder
dCode-Auditor   deleted-dcode   added-dcode hcpc-coder  hcpc-auditor    deletedhcpc addedhcpc   mod-coder   mod-auditor deletedmod  addedmod
 7111   89321   7832,900114 78832,900114    7832    78832   Y564,U3321,U1111,H8811,J8923    Y564,U3321,U41111,H8811,J8923   U1111   U41111  5,1 8,1 5   8   67,26   67,126  26  126

Can anyone please help me here任何人都可以在这里帮助我吗

sql Tried sql 试过

with cte4 as
  
(
  select info:dCode as dtcode
  from cte3, lateral flatten( input => saveinfo:dgoses )
)
select  dCode from cte4, lateral flatten( input => dtcode )

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.. 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..

    with I as

( 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, ( select ,dense_rank() over(按 Billid 排序,Ctextid)作为 tid,dense_rank() over(按 Billid 分区,Ctextid 排序按 Created )作为 n 来自 ##input1 ),D 为( select I.k , ] 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,

 (
     select string_agg(cCode, ',') within group (order by mk)
     from 
     (
         select distinct cCode, mk
         from C
         where tid = t.tid
     ) d
 ) as cCodeCoder,
 (
     select string_agg(cCode, ',') within group (order by mk)
     from 
     (
         select distinct cCode, mk
         from A
         where tid = t.tid
     ) d
 ) as cCodeAuditor,
 (
     select string_agg(cCode, ',')
     from
     (
         select cCode
         from C
         where tid = t.tid
         except
         select cCode
         from A 
         where tid = t.tid
     ) d
 ) as deletedcCode,
 (
     select string_agg(cCode, ',')
     from
     (
         select cCode
         from A
         where tid = t.tid
         except
         select cCode
         from C
         where tid = t.tid
     ) d
 ) as addedcCode,

 (
     select string_agg(dCode, ',') within group (order by mk, dk)
     from 
     (
         select distinct dCode, mk, dk
         from C
         where tid = t.tid
     ) d
 ) as dCodeCoder,
 (
     select string_agg(dCode, ',') within group (order by mk, dk)
     from 
     (
         select distinct dCode, mk, dk
         from A
         where tid = t.tid
     ) d
 ) as dCodeAuditor,
 (
     select string_agg(dCode, ',')
     from
     (
         select dCode
         from C
         where tid = t.tid
         except
         select dCode
         from A 
         where tid = t.tid
     ) d
 ) as deleteddCode,
 (
     select string_agg(dCode, ',')
     from
     (
         select dCode
         from A
         where tid = t.tid
         except
         select dCode
         from C
         where tid = t.tid
     ) d
 ) as addeddCode

from I as t where n = 1从 I 作为 t 其中 n = 1

Thanks, Arun谢谢,阿伦

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).我不完全确定您如何需要数据,但您正在尝试获取“cCode、dCode、hcps 和 mod”(假设 hcps 实际上是 hcpc)。 The problem is cCode, hcpc, and mod are all on the same level of the JSON.问题是 cCode、hcpc 和 mod 都在 JSON 的同一级别。 dCode is not. dCode 不是。 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.这可以展平为具有 1:MANY 关系的两个表,或者可以展平为一个重复 cCode、hcpc 和 mod 值的表。 This example shows the second option:此示例显示了第二个选项:

-- I created a table named FOO and added your JSON as a variant
create temp table foo(v variant);

with
JSON(C_CODE, DGOSES, HCPC, "MOD") as
(
select   "VALUE":cCode::int         as C_CODE
        ,"VALUE":dgoses             as DGOSES
        ,"VALUE":hcpc::int          as HCPS
        ,"VALUE":mod::int           as "MOD"
from foo, lateral flatten(v)
)
select C_CODE, HCPC, "MOD", "VALUE":dCode::string as D_CODE
from JSON, lateral flatten(DGOSES);

This creates a table like this:这将创建一个像这样的表:

C_CODE    HCPC    MOD    D_CODE
  7832       5     67    Y564
  7832       5     67    U3321
  7832       5     67    U1111 
  7832       5     67    HBB11
900114       1     26    J8923

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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