我有一个XML,我想在该位置获取scheduleName的值,其中GrantName = Grant 2和Scheduleid = 003。
我还将结果与包含客户信息的表结合在一起。对于每个客户ID,我都希望获取提及的计划名称。
下面是我正在使用的示例XML。
DECLARE @tbl TABLE (XmlCol xml)
INSERT INTO @tbl VALUES
('<option>
<OptionName>Option 1</OptionName>
<grant>
<GrantName>Grant 1</GrantName>
<schedules>
<schedule>
<scheduleID id=001></scheduleID>
<scheduleName>s1</scheduleName>
<scheduleDate>1/1/2018</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID id=002></scheduleID>
<scheduleName>s2</scheduleName>
<scheduleDate>2/1/2018</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID id=003></scheduleID>
<scheduleName>s3</scheduleName>
<scheduleDate>3/1/2018</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
</schedules>
</grant>
<grant>
<GrantName>Grant 2</GrantName>
<schedules>
<schedule>
<scheduleID id=001></scheduleID>
<scheduleName>s1</scheduleName>
<scheduleDate>1/1/2019</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID id=002></scheduleID>
<scheduleName>s2</scheduleName>
<scheduleDate>2/1/2019</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID id=003></scheduleID>
<scheduleName>s3</scheduleName>
<scheduleDate>3/1/2019</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
</schedules>
</grant>
<grant>
<GrantName>Grant 3</GrantName>
<schedules>
<schedule>
<scheduleID id=001></scheduleID>
<scheduleName>s1</scheduleName>
<scheduleDate>1/1/2020</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID id=002></scheduleID>
<scheduleName>s2</scheduleName>
<scheduleDate>2/1/2020</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID id=003></scheduleID>
<scheduleName>s3</scheduleName>
<scheduleDate>3/1/2020</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
</schedules>
</grant>
</option>'
)
请帮助我进行sql查询
答案 0 :(得分:0)
DECLARE @tbl TABLE (XmlCol xml)
INSERT INTO @tbl VALUES
('<Fact>
<ID Value="d71a58e9-6e9c-4c2b-9a6a-76e066978af4" />
<AssetAllocationModel>
<ModelPortfolios>
<ModelPortfolio>
<Name>Asset Pres</Name>
<ModelPortfolioID Value="assetpres" />
<AssetClassMix>
<Percent>0.1000</Percent>
<AssetClassID Value="largegrowth" />
<Value />
</AssetClassMix>
<AssetClassMix>
<Percent>0.0500</Percent>
<AssetClassID Value="largevalue" />
<Value />
</AssetClassMix>
<AssetClassMix>
<Percent>0.2500</Percent>
<AssetClassID Value="investbond" />
<Value />
</AssetClassMix>
<AssetClassMix>
<Percent>0.5000</Percent>
<AssetClassID Value="shortermbond" />
<Value />
</AssetClassMix>
<AssetClassMix>
<Percent>0.1000</Percent>
<AssetClassID Value="cash" />
<Value />
</AssetClassMix>
<Risk>0</Risk>
</ModelPortfolio>
<ModelPortfolio>
<Name>Income</Name>
<ModelPortfolioID Value="income" />
<AssetClassMix>
<Percent>0.3000</Percent>
<AssetClassID Value="investbond" />
<Value />
</AssetClassMix>
<AssetClassMix>
<Percent>0.3500</Percent>
<AssetClassID Value="shortermbond" />
<Value />
</AssetClassMix>
<AssetClassMix>
<Percent>0.0500</Percent>
<AssetClassID Value="cash" />
<Value />
</AssetClassMix>
<Risk>0</Risk>
</ModelPortfolio>
<ModelPortfolio>
<Name>Agg Growth</Name>
<ModelPortfolioID Value="aggrgrowth" />
<AssetClassMix>
<Percent>0.1000</Percent>
<AssetClassID Value="internat" />
<Value />
</AssetClassMix>
<AssetClassMix>
<Percent>0.1000</Percent>
<AssetClassID Value="emerging" />
<Value />
</AssetClassMix>
<AssetClassMix>
<Percent>0.0300</Percent>
<AssetClassID Value="highyldbond" />
<Value />
</AssetClassMix>
<AssetClassMix>
<Percent>0.0200</Percent>
<AssetClassID Value="cash" />
<Value />
</AssetClassMix>
<Risk>0</Risk>
</ModelPortfolio>
</ModelPortfolios>
</AssetAllocationModel>
<AttestationDate />
</Fact>'
)
select * from @tbl
SELECT top 1 t.x.value('(./Percent)[1]','decimal(10,2)')*100 FROM(
SELECT t.x.query('(./AssetClassMix)') as 'AssetClassMix'
FROM @tbl rec
CROSS APPLY rec.XmlCol.nodes ('/Fact/AssetAllocationModel/ModelPortfolios/ModelPortfolio') t(x)
WHERE t.x.value('(./ModelPortfolioID/@Value)[1]','varchar(max)') = 'aggrgrowth'
) a
CROSS APPLY a.AssetClassMix.nodes('/AssetClassMix') t(x)
WHERE t.x.value('(./AssetClassID/@Value)[1]','varchar(max)')='cash'
这就是我用来获取价值的东西。有没有其他方法,而无需使用CROSS APPLY。由于PROD中有大量数据,因此CROSS APPLY非常繁重。