具有深度嵌套数组循环的 SQL 查询 OpenJson

Posted

技术标签:

【中文标题】具有深度嵌套数组循环的 SQL 查询 OpenJson【英文标题】:SQL query OpenJson with deep nested arrays loop 【发布时间】:2020-01-14 09:54:29 【问题描述】:

我知道这里有很多类似的问题和答案。我已经阅读了其中的大部分内容,但我无法查询 JSON 结构中的嵌套数组。我迷失在 CROSS APPLY 中。

我实际上是在查询一个 Web API,但为了我的问题,我把它放在一个变量中。 我正在尝试将 ID (3519) 和“worker_contracts”的所有信息插入到表中。

declare @json nvarchar(max)
set @json = '


    "data": [
        
            "id": "3519",
            "type": "affiliate_workers",
            "attributes": 
                "title": "mr",
                "first_name": "John",
                "last_name": "Doe",
                "telephone": "+32 471 12 34 56",
                "worker_contracts": [
                    
                        "start_date": "2020-01-06",
                        "end_date": null,
                        "social_secretary_specific_data": 
                            "affiliate_id": 54,
                            "worker_details_id": 3378,
                            "start_date": "2020-01-06",
                            "end_date": null,
                            "affiliate_worker_id": 3519,

                        ,
                        "work_hours_per_week": 25.0,
                        "comment": "",
                        "roster_week": [
                            
                                "start_date": "2020-01-13",
                                "number_hours": 25
                            ,
                            
                                "start_date": "2020-01-06",
                                "number_hours": 25
                            
                        ],
                        "social_secretary_identifier": "123456"
                    ,
                    
                        "start_date": "2019-09-23",
                        "end_date": "2020-01-05",
                        "social_secretary_specific_data": 
                            "affiliate_id": 54,
                            "worker_details_id": 3378,
                            "start_date": "2019-09-23",
                            "end_date": "2020-01-05",
                            "affiliate_worker_id": 3519,
                        ,
                        "work_hours_per_week": 21.0,
                        "comment": "",
                        "roster_week": [
                            
                                "start_date": "2019-09-30",
                                "number_hours": 21
                            ,
                            
                                "start_date": "2019-09-23",
                                "number_hours": 21
                            
                        ],
                        "social_secretary_identifier": "123456"
                    
                ],
                "sodexo_reference": "56789",
                "region": "Vlaanderen",
                "identity_card_number": "A1122334455",
                "can_work_with_animals": false
            
        

    ]


My SQL 查询只选择开始和结束日期值。我不明白如何添加其他嵌套数据。

insert into AffiliateWorkersContract_WRK
select WA.id, wc.*
from OpenJson((CAST(@json as nvarchar(max))),'$.data')


WITH (
id int,
worker_contracts nvarchar(max) as json
)
as WA

cross apply openjson (WA.worker_contracts)

with
(
start_date date '$.start_date',
end_date date '$.end_date',


) as WC

提前感谢大家的帮助。

【问题讨论】:

【参考方案1】:

自己找到了解决方案。我只需要继续使用十字架申请


insert into AffiliateWorkersContract_WRK
select WA.id, wawc.start_date, wawc.end_date, wawc.signing_date, wawc.contract_variability,
wawc.vehicle_type, wawc.addendum, wawc.work_hours_per_week,wawc.comment, wawc.social_secretary_identifier,
wssd.affiliate_id, wssd.worker_details_id, wssd.created_at, wssd.updated_at, wssd.affiliate_worker_id, wssd.comment, wssd.author_id,
wssd.parent_id, wssd.operating_headquarter_id, wssd.reference_period, wssd.varvar_ref_period_avg_minutes_per_week,
wssd.varvar_min_work_minutes_per_week, wssd.varvar_max_work_minutes_per_week

from OpenJson((CAST(@pootsy_AWWC_request as nvarchar(max))),'$.data')


WITH (
id int,
attributes nvarchar(max) as json
) as WA

cross apply openjson ((CAST(WA.attributes as nvarchar(max))))

WITH (
worker_contracts nvarchar(max) as json
) as WC

cross apply OpenJson ((CAST(WC.worker_contracts as nvarchar(max))))

WITH (
start_date date,
end_date date,
signing_date date,
contract_variability varchar(20),
vehicle_type varchar(20),
addendum varchar(10),
work_hours_per_week decimal(4,2),
comment nvarchar(max),
social_secretary_identifier int,
social_secretary_specific_data nvarchar(max) as json,
roster_week nvarchar(max) as json
) AS WAWC

cross apply OpenJson (WAWC.social_secretary_specific_data)

WITH (
affiliate_id int,
worker_details_id int,
created_at varchar(40),
updated_at varchar(40),
affiliate_worker_id int,
comment nvarchar(max),
author_id int,
parent_id int,
operating_headquarter_id int,
reference_period varchar(10),
varvar_ref_period_avg_minutes_per_week decimal(4,2),
varvar_min_work_minutes_per_week decimal(4,2),
varvar_max_work_minutes_per_week decimal(4,2)
) AS WSSD

【讨论】:

以上是关于具有深度嵌套数组循环的 SQL 查询 OpenJson的主要内容,如果未能解决你的问题,请参考以下文章

带有连接的 SQL 查询以获取嵌套的对象数组

Ramda js:具有嵌套对象数组的深度嵌套对象的镜头

MongoDB Mongoose 聚合查询深度嵌套数组删除空结果并填充引用

具有三层深度嵌套模型的查询集过滤器(多个一对多关系)

具有深度嵌套层次结构的不可变 NSDictionary:更改键的值?

从数据数组(php,mysql)编写循环sql查询