0

我将以下 xml 加载到具有变体数据类型的表中

<VehicleDamageEstimateAddRq xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.cieca.com/BMS">
    <RqUID>014b91da-1227-4c2b-995f-0f2f6e1b0be4</RqUID>
    <DamageLineInfo>
        <LineNum>2</LineNum>
        <UniqueSequenceNum>4</UniqueSequenceNum>
        <ParentLineNum>3</ParentLineNum>
        <SupplementNum>0</SupplementNum>
        <EstimateVerCode>EM</EstimateVerCode>
        <ManualLineInd>false</ManualLineInd>
        <AutomatedEntry>true</AutomatedEntry>
        <LineStatusCode>1</LineStatusCode>
        <LineDesc>O/H front bumper</LineDesc>
        <DescJudgmentInd>false</DescJudgmentInd>
        <LaborInfo>
            <LaborType>LAB</LaborType>
            <DatabaseLaborType>LAB</DatabaseLaborType>
            <LaborOperation>OP0</LaborOperation>
            <LaborHours>3.9</LaborHours>
            <DatabaseLaborHours>3.9</DatabaseLaborHours>
            <LaborInclInd>false</LaborInclInd>
            <LaborAmt>195.00</LaborAmt>
            <TaxableInd>false</TaxableInd>
            <LaborHoursJudgmentInd>false</LaborHoursJudgmentInd>
            <LaborTypeJudgmentInd>false</LaborTypeJudgmentInd>
            <DrillingInd>false</DrillingInd>
        </LaborInfo>
    </DamageLineInfo>
    <DamageLineInfo>
        <LineNum>3</LineNum>
        <UniqueSequenceNum>2</UniqueSequenceNum>
        <SupplementNum>0</SupplementNum>
        <EstimateVerCode>EM</EstimateVerCode>
        <ManualLineInd>false</ManualLineInd>
        <AutomatedEntry>false</AutomatedEntry>
        <LineStatusCode>1</LineStatusCode>
        <LineDesc>Bumper cover w/o hybrid</LineDesc>
        <DescJudgmentInd>false</DescJudgmentInd>
        <PartInfo>
            <PartType>PAA</PartType>
            <PartNum>MI1000341</PartNum>
            <OEMPartNum>6400L177</OEMPartNum>
            <NonOEM>
                <PartType>PAA</PartType>
                <NonOEMPartNum>MI1000341</NonOEMPartNum>
                <PartSelectedInd>true</PartSelectedInd>
            </NonOEM>
            <PartPrice>431.00</PartPrice>
            <UnitPartPrice>431.00</UnitPartPrice>
            <TaxableInd>true</TaxableInd>
            <PriceJudgmentInd>false</PriceJudgmentInd>
            <AlternatePartInd>true</AlternatePartInd>
            <GlassPartInd>false</GlassPartInd>
            <PriceInclInd>false</PriceInclInd>
            <Quantity>1</Quantity>
        </PartInfo>
        <LaborInfo>
            <LaborType>LAB</LaborType>
            <DatabaseLaborType>LAB</DatabaseLaborType>
            <LaborOperation>OP11</LaborOperation>
            <LaborHours>0.0</LaborHours>
            <DatabaseLaborHours>3.9</DatabaseLaborHours>
            <LaborInclInd>true</LaborInclInd>
            <LaborAmt>0</LaborAmt>
            <TaxableInd>false</TaxableInd>
            <LaborHoursJudgmentInd>false</LaborHoursJudgmentInd>
            <LaborTypeJudgmentInd>false</LaborTypeJudgmentInd>
            <DrillingInd>false</DrillingInd>
        </LaborInfo>
        <RefinishLaborInfo>
            <LaborType>LAR</LaborType>
            <LaborOperation>OP6</LaborOperation>
            <LaborHours>3.1</LaborHours>
            <DatabaseLaborHours>3.1</DatabaseLaborHours>
            <LaborInclInd>false</LaborInclInd>
            <LaborAmt>155.00</LaborAmt>
            <LaborHoursJudgmentInd>false</LaborHoursJudgmentInd>
        </RefinishLaborInfo>
        <OtherChargesInfo>
            <OtherChargesType>MAPA</OtherChargesType>
            <Price>102.30</Price>
            <UnitOfMeasure>HR</UnitOfMeasure>
            <Quantity>3.1</Quantity>
            <PriceInclInd>false</PriceInclInd>
        </OtherChargesInfo>
    </DamageLineInfo>
</VehicleDamageEstimateAddRq>

DamageLineInfo 标记/部分重复 N 次,但内部键对于所有 DamageLineInfo 标记/部分都不相同,因为您可以看到 LineNum:3 具有 LineNum:2 没有的 RefinishLaborInfo 和 OtherChargesInfo。我编写了下面的 sql 以提取带有外部 => True 的两行,但它只返回 1 行(行号:3)

SELECT GET(XMLGET(DamageLineInfo.value, 'LineNum'),'$')::number     as DamageLineInfo_LineNum
,GET(XMLGET(DamageLineInfo.value, 'UniqueSequenceNum'),'$')::number     as DamageLineInfo_UniqueSequenceNum
,
GET(XMLGET(RefinishLaborInfo.value, 'LaborType'),'$')::string     as RefinishLaborInfo_LaborType
FROM "DEFAULT"."PUBLIC"."SAMPLE2_XML"
,LATERAL FLATTEN(GET(SRC_XML, '$')) DamageLineInfo
,LATERAL FLATTEN(input=>GET(DamageLineInfo.value, '$'),OUTER=>TRUE) RefinishLaborInfo
where 
GET(DamageLineInfo.value, '@') = 'DamageLineInfo'and
GET(RefinishLaborInfo.value,'@')='RefinishLaborInfo'

即使某些部分(例如 RefinishLaborInfo )不存在,我的代码应该是什么来获取这两行。

4

1 回答 1

0

我发现在 Snowflake 中使用 CTE 最适合解析嵌套的 XML 文档。

with DAMAGE_LINE_INFO as
(
    select THIS from SAMPLE2_XML, lateral flatten(SRC_XML, recursive => true) where VALUE = 'DamageLineInfo'
),
DAMAGE_AND_LABOR as
(
    select   get(xmlget(THIS, 'LineNum'), '$')              as "DamageLineInfo_LineNum"
            ,get(xmlget(THIS, 'UniqueSequenceNum'), '$')    as "DamageLineInfo_UniqueSequenceNum"
            ,xmlget(THIS, 'LaborInfo')                      as LABOR_INFO
            ,xmlget(THIS, 'RefinishLaborInfo')              as REFINISH_LABOR_INFO
    from    DAMAGE_LINE_INFO
)
select       "DamageLineInfo_LineNum"
            ,"DamageLineInfo_UniqueSequenceNum"
            ,get(xmlget(LABOR_INFO, 'LaborType'), '$')::string           as LABOR_TYPE
            ,get(xmlget(REFINISH_LABOR_INFO, 'LaborType'), '$')::string  as REFINISH_LABOR_TYPE
from DAMAGE_AND_LABOR
于 2021-08-13T00:27:58.490 回答