如何加入2个table,其中一个table中的数据有JSON个数据需要解析?

How to join 2 tables, where data in one table has JSON data that needs to be parsed?

我正在努力处理一些 SQL,它们应该将来自两个 table 的数据连接在一起,但我还需要将 JSON 数组解析为行输出 table...

以下是我想加入的 2 个 table 示例:

订单

Order_Number Delivery_Date
1 2020-05-18 07:00:00.000
2 2020-08-31 07:30:00.000

和历史:

Order_Number History_Details
1 [{"FieldName":"OrderStatusType.Description","FieldType":"String","ValueBefore":"Delivered","ValueAfter":"Undelivered","Action":2}]
1 [{"FieldName":"VoucherCode","FieldType":"String","ValueBefore":"","ValueAfter":"64646456","Action":1},{"FieldName":"PricingType.Description","FieldType":"String","ValueBefore":"Standard","ValueAfter":"Discount","Action":2}]
2 [{"FieldName":"InitialComment","FieldType":"String","ValueBefore":"","ValueAfter":"Test Comment","Action":2},{"FieldName":"Appointment.Date","FieldType":"DateTime","ValueBefore":"2020-08-06T07:30:00.000","ValueAfter":"2020-08-31T07:30:00.000","Action":0}]
2 null

(不幸的是,这是一个字符串“null”,而不是 NULL - 我们在加入数据时必须处理)

我想要的是这样的输出:

Order Number Delivery Date FieldName ValueBefore ValueAfter
1 2020-05-18 07:00:00.000 OrderStatusType.Description Delivered Undelivered
1 2020-05-18 07:00:00.000 VoucherCode 64646456
1 2020-05-18 07:00:00.000 PricingType.Description Standard Discount
2 2020-08-31 07:30:00.000 InitialComment Test Comment
2 2020-08-31 07:30:00.000 Appointment.Date 2020-08-06T07:30:00.000 2020-08-31T07:30:00.000

我可以自己做这 2 个查询,我只是在努力加入他们...

即这给了我没有 JSON 分解的一切:

SELECT
  o.order_number as [Order Number],
  o.delivery_date as [Delivery Date],
  oh.history_details as [History]
FROM [dbo].[Order] o
JOIN [dbo].[History] oh on oh.order_number = o.order_number
WHERE oh.history_details != 'null'

虽然我可以做 JSON 映射,例如:

DECLARE @json NVARCHAR(MAX)
SET @json='[{"FieldName":"VoucherCode","FieldType":"String","ValueBefore":"","ValueAfter":"64646456","Action":1},{"FieldName":"PricingType.Description","FieldType":"String","ValueBefore":"Standard","ValueAfter":"Discount","Action":2}]';
SELECT *
FROM OPENJSON(@json)
WITH (   
   FieldName   varchar(200) '$.FieldName' ,  
   ValueBefore     varchar(200)     '$.ValueBefore',  
   ValueAfter varchar(200) '$.ValueAfter'  
 ) 

我真的很想知道如何将这两者结合起来并得到我想要的结果,很多 OPENJSON 例子都像我上面的例子,它们看起来期待一个 column/datatype.

你这里的问题有点问题,因为你想要的输出与你问题的其余部分相矛盾。同一列中不能有不同的数据类型,因此在为每个值保留适当的数据类型的同时无法获得所需的输出。

您的选择是要么将数据保留为文本字符串,因为它显示在 JSON 中,要么为可以捕获的每种可能的数据类型设置多个列。

如果这只是一个日志 table 来捕获您不会定期查询的更改历史记录,您可能只需将值保留为文本字符串即可。


查询

declare @Order table(Order_Number int,Delivery_Date datetime);
insert into @Order values
 (1,'2020-05-18 07:00:00.000')
,(2,'2020-08-31 07:30:00.000')
;

declare @History table(Order_Number int, History_Details varchar(max));
insert into @History values
 (1,'[{"FieldName":"OrderStatusType.Description","FieldType":"String","ValueBefore":"Delivered","ValueAfter":"Undelivered","Action":2}]')
,(1,'[{"FieldName":"VoucherCode","FieldType":"String","ValueBefore":"","ValueAfter":"64646456","Action":1},{"FieldName":"PricingType.Description","FieldType":"String","ValueBefore":"Standard","ValueAfter":"Discount","Action":2}]')
,(2,'[{"FieldName":"InitialComment","FieldType":"String","ValueBefore":"","ValueAfter":"Test Comment","Action":2},{"FieldName":"Appointment.Date","FieldType":"DateTime","ValueBefore":"2020-08-06T07:30:00.000","ValueAfter":"2020-08-31T07:30:00.000","Action":0}]')
,(2,'null')
;

select o.Order_Number
      ,o.Delivery_Date
      ,j.FieldName
      ,j.ValueBefore
      ,j.ValueAfter

      ,case when j.FieldType = 'String' then j.ValueBefore end as ValueBeforeString
      ,case when j.FieldType = 'DateTime' then try_convert(datetime,j.ValueBefore,127) end as ValueBeforeDateTime
      ,case when j.FieldType = 'String' then j.ValueAfter end as ValueAfterString
      ,case when j.FieldType = 'DateTime' then try_convert(datetime,j.ValueAfter,127) end as ValueAfterDateTime
from @Order as o
    join @History as h
        on o.Order_Number = h.Order_Number
    outer apply openjson(h.History_Details)
                with (FieldName   varchar(200) '$.FieldName' 
                     ,FieldType   varchar(200) '$.FieldType'
                     ,ValueBefore varchar(200) '$.ValueBefore'
                     ,ValueAfter  varchar(200) '$.ValueAfter'
                     ) as j
where h.History_Details <> 'null'
order by o.Order_Number
        ,o.Delivery_Date;

输出

Order_Number Delivery_Date FieldName ValueBefore ValueAfter ValueBeforeString ValueBeforeDateTime ValueAfterString ValueAfterDateTime
1 2020-05-18 07:00:00.000 OrderStatusType.Description Delivered Undelivered Delivered NULL Undelivered NULL
1 2020-05-18 07:00:00.000 VoucherCode 64646456 NULL 64646456 NULL
1 2020-05-18 07:00:00.000 PricingType.Description Standard Discount Standard NULL Discount NULL
2 2020-08-31 07:30:00.000 InitialComment Test Comment NULL Test Comment NULL
2 2020-08-31 07:30:00.000 Appointment.Date 2020-08-06T07:30:00.000 2020-08-31T07:30:00.000 NULL 2020-08-06 07:30:00.000 NULL 2020-08-31 07:30:00.000

您可以使用 OUTER APPLY 将数据从 History table 推送到 OPENJSON.

我们可以使用 NULLIF 来消除任何表示字符串 'null'

的 JSON
SELECT
  o.order_number as [Order Number],
  o.delivery_date as [Delivery Date],
  j.FieldName,
  j.ValueBefore,
  j.ValueAfter
FROM [dbo].[Order] o
JOIN [dbo].[History] oh on oh.order_number = o.order_number
OUTER APPLY OPENJSON(NULLIF(oh.history_details, 'null'))
WITH (   
   FieldName   varchar(200) '$.FieldName' ,  
   ValueBefore     varchar(200)     '$.ValueBefore',  
   ValueAfter varchar(200) '$.ValueAfter'  
 ) j

正如其他人所提到的,同一列中不能有不同的数据类型,因此它必须保持 varchar