0
我有一個XML列(沒有輸入)呼籲xml_response扁平化的XML數據
<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和一些不以下XML數據
我已經寫了下面的查詢:即使
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:計費要素不包含v:TieredCharge重複元素?
例如,我得到4行,其中電荷類型是QAC,即使只有一個這種類型的元素?
我該如何查詢,以便每個Charge元素只有一行,除非它下面有重複的子元素,在這種情況下,我只會得到每個元素重複的行?
所以我應該得到的東西是這樣的:
MaintChange TieredCharge1 row
MaintChange TieredCharge2 row
MaintChange TieredCharge3 row
MaintChange TieredCharge4 row
MaintCharge
MaintCharge
QAC