2013-05-27 46 views
1

我在TSQL中看起来像这样的大表中有一个XML列,我如何计算属性(LCPID)发生的次数?统计属性的出现次数?

感谢,


代码:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <PingAutoRequest xmlns="http://Reply.LeadMarketPlace.Services.Ping.ServiceContracts/2007/10/ServiceContracts"> 
     <AutomotiveLead> 
     <LCPId>766</LCPId> 
     <Zipcode>33544</Zipcode> 
     <Make>Ford</Make> 
     <Model>Escape</Model> 
     <LeadType>New</LeadType> 
     <Year>2013</Year> 
     <Trim>FWD S</Trim> 
     <ExteriorColor /> 
     <InteriorColor /> 
     <Transmission /> 
     <TradeIn>false</TradeIn> 
     <LastFourPhoneDigits /> 
     <LastName /> 
     </AutomotiveLead> 
    </PingAutoRequest> 
    </soap:Body> 
</soap:Envelope> 

回答

2

OK,因为你的XML不包含任何attributes(除了命名空间)我会假设你的意思是你想来计算<LCPId>elements的数量。如果是这样,那么你可以做的是这样的...

;WITH XMLNAMESPACES ('http://Reply.LeadMarketPlace.Services.Ping.ServiceContracts/2007/10/ServiceContracts' as ns) 
SELECT XmlColumn.value('(count(//ns:LCPId))','int') 
FROM YourTableName 

注意,我们需要处理的XML namespace using the WITH statement,分号是不是一个错误。然后我们用XPath表达式count(//ns:LCPId)来计算元素的数量。

您可以在SQL小提琴here上看到它的实际操作。