将财政家庭提取为笛卡尔积(即无约束连接)子查询。这实际上充当行级函数,将表达式附加到源输出。
请注意,我添加了一个新的第三个受益人以及新的fiscal_partners,以证明参照完整性得到了维护。
with xml_test as (
select
'<!--Build-Version: 1.564-REVb37dd024a8-20220530T1418--><tns:Message xsi:schemaLocation="https://appprod.be/Trivia/documents/Docs/XSDs/v1/T013 T013_v1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="https://appprod.be/Trivia/documents/Docs/XSDs/v1/T013">
<Header>
<Agent>90015299</Agent>
<SocialFund>299</SocialFund>
<MessageId>T013</MessageId>
<Timestamp RequestDate="2022-06-16T23:20:00" ProcessDate="2022-06-16T23:25:20" ResponseDate="2022-06-16T23:25:20"/>
<Reference NRK="6e6806740a6ea06d7ca6ae7b31edeb8e" Requestor="90010299"/>
<Result>OK</Result>
<ReturnCode>ResponseOnly</ReturnCode>
</Header>
<Body>
<Request>
<TransmissionMethod>BULK</TransmissionMethod>
<SocialFundCode>299</SocialFundCode>
<FileNumber>9913894</FileNumber>
<FiscalYear>2020</FiscalYear>
<FiscalHouseHolds>
<FiscalHouseHold>
<Beneficiary>66090999038</Beneficiary>
<FiscalPartners>
<FiscalPartner>65120999956</FiscalPartner>
</FiscalPartners>
</FiscalHouseHold>
<FiscalHouseHold>
<Beneficiary>66090999038</Beneficiary>
<FiscalPartners>
<FiscalPartner>65120999956</FiscalPartner>
<FiscalPartner>94013099924</FiscalPartner>
</FiscalPartners>
</FiscalHouseHold>
<FiscalHouseHold>
<Beneficiary>66090999040</Beneficiary>
<FiscalPartners>
<FiscalPartner>65120990056</FiscalPartner>
<FiscalPartner>94013000924</FiscalPartner>
</FiscalPartners>
</FiscalHouseHold>
</FiscalHouseHolds>
</Request>
</Body>
</tns:Message>' as x
)
SELECT
(xpath('/tnsMessage/Body/Request/FileNumber/text()', replace(x, ':','')::xml))[1]::text file_number
, (xpath('//Beneficiary/text()', fiscal_household::xml))[1]::text AS benef
, array_to_string( (xpath('//FiscalPartner/text()', fiscal_household::xml)), ' - ') fiscal_partners
FROM xml_test, (SELECT unnest(xpath('/tnsMessage/Body/Request/FiscalHouseHolds/FiscalHouseHold', replace(x, ':','')::xml))::text AS fiscal_household FROM xml_test) A ;
产量
文件编号 | 好处 | 财政合作伙伴 |
---|---|---|
9913894 | 66090999038 | 65120999956 |
9913894 | 66090999038 | 65120999956 - 94013099924 |
9913894 | 66090999040 | 65120990056 - 94013000924 |
需要注意的是,仅仅因为 2 阶段查询与 3 阶段查询在语法上可能更清晰,查询优化器最终可能仍会构建相同的计划。