我在xml列(非类型)中有以下xml数据,名为xml_response
<SEIContent>
<Request>
<eq:Charge>
<v:Type>MaintCharge</v:Type>
<v:Term>0</v:Term>
<v:StartMonth>0</v:StartMonth>
<v:EndMonth>0</v:EndMonth>
<v:Rate>0</v:Rate>
<v:RebateRatio>0</v:RebateRatio>
<v:MaxRebate>0</v:MaxRebate>
<v:TieredCharge>
<v:Term>0</v:Term>
<v:Rate>0.75</v:Rate>
<v:LowerBand>0</v:LowerBand>
<v:UpperBand>249999.99</v:UpperBand>
<v:BandCurrency>GBP</v:BandCurrency>
</v:TieredCharge>
<v:TieredCharge>
<v:Term>0</v:Term>
<v:Rate>0.7</v:Rate>
<v:LowerBand>250000</v:LowerBand>
<v:UpperBand>499999.99</v:UpperBand>
<v:BandCurrency>GBP</v:BandCurrency>
</v:TieredCharge>
<v:TieredCharge>
<v:Term>0</v:Term>
<v:Rate>0.6</v:Rate>
<v:LowerBand>500000</v:LowerBand>
<v:UpperBand>999999.99</v:UpperBand>
<v:BandCurrency>GBP</v:BandCurrency>
</v:TieredCharge>
<v:TieredCharge>
<v:Term>0</v:Term>
<v:Rate>0.5</v:Rate>
<v:LowerBand>1000000</v:LowerBand>
<v:UpperBand>9999999.99</v:UpperBand>
<v:BandCurrency>GBP</v:BandCurrency>
</v:TieredCharge>
</eq:Charge>
<eq:Charge>
<v:Type>MaintCharge</v:Type>
<v:Term>0</v:Term>
<v:StartMonth>0</v:StartMonth>
<v:EndMonth>59</v:EndMonth>
<v:Rate>1.5</v:Rate>
<v:RebateRatio>0</v:RebateRatio>
<v:MaxRebate>0</v:MaxRebate>
</eq:Charge>
<eq:Charge>
<v:Type>MaintCharge</v:Type>
<v:Term>0</v:Term>
<v:StartMonth>60</v:StartMonth>
<v:EndMonth>0</v:EndMonth>
<v:Rate>0.5</v:Rate>
<v:RebateRatio>0</v:RebateRatio>
<v:MaxRebate>0</v:MaxRebate>
</eq:Charge>
<eq:Charge>
<v:Type>QAC</v:Type>
<v:Basis>FixedAmount</v:Basis>
<v:Term>0</v:Term>
<v:StartMonth>0</v:StartMonth>
<v:EndMonth>0</v:EndMonth>
<v:Rate>105</v:Rate>
<v:RebateRatio>0</v:RebateRatio>
<v:MaxRebate>0</v:MaxRebate>
</eq:Charge>
</Request>
<Response>
<eq:Ref>QV00000393</eq:Ref>
</Response>
</SEIContent>
所以你会注意到一些费用包含重复元素TieredCharge而有些则不包含
我写了以下查询:
WITH XMLNAMESPACES('http://lu/blah' AS eq, 'http://lu/blah2' AS v,
DEFAULT 'http://lu/blah3'
SELECT
nref.value('Response[1]/eqRef[1]', 'nvarchar(200)') Ref,
ncharge.value('v:Type[1]', 'nvarchar(50)') ChargeType,
ncharge.value('v:Basis[1]', 'nvarchar(50)') ChargeBasis,
ncharge.value('v:Term[1]', 'int') Term,
ncharge.value('v:StartMonth[1]', 'int') StartMonth,
ncharge.value('v:EndMonth[1]', 'int') EndMonth,
ncharge.value('v:Rate[1]', 'money') Rate,
ncharge.value('v:RebateRatio[1]', 'money') RebateRatio,
ncharge.value('v:MaxRebate[1]', 'money') MaxRebate,
tcharge.value('v:Term[1]', 'int') TieredTerm,
tcharge.value('v:Rate[1]', 'money') TieredRate,
tcharge.value('v:LowerBand[1]', 'money') TieredLowerBand,
tcharge.value('v:UpperBand[1]', 'money') TieredUpperBand
INTO #TempCharges
FROM xml_response CROSS APPLY response_body.nodes('//SEIContent') AS Quote(nref)
CROSS APPLY response_body.nodes('//Request//eq:Charge') AS Charge(ncharge)
CROSS APPLY response_body.nodes('//Request//eq:Charge//v:TieredCharge') AS TieredCharge(tcharge)
WHERE nref.value('Request[1]/eq:Product[1]', 'nvarchar(60)') <> 'AVL'
select * from #TempCharges
所以虽然这会使xml变平,但即使eq:Charge元素不包含v:TieredCharge重复元素,我得到的也是重复行?
例如,我得到4行,其中费用类型是QAC,即使只有一个具有该类型的元素?
我如何查询这个,这样我每个Charge元素只能获得一行,除非它下面有重复的子元素,在这种情况下我只会为每个元素重复行?
所以我应该得到这样的东西:
MaintChange TieredCharge1 row
MaintChange TieredCharge2 row
MaintChange TieredCharge3 row
MaintChange TieredCharge4 row
MaintCharge
MaintCharge
QAC
答案 0 :(得分:0)
所以诀窍是在查询的最后一位使用OUTER APPLY - 它就像一个内连接。然后就行了。