简体   繁体   English

按属性过滤 SQL Server XML 并返回元素的所有匹配属性值

[英]Filter SQL Server XML by Attribute and return all matching attribute value for elements

I have the following XML:我有以下 XML:

<Games>
  <Game>
    <Place City="LAS" State="NV" />
    <Place City="ATL" State="GA" />
    <Store Store_ID="12" Price_ID="166" Description="Demons v3" />
    <Store Store_ID="12" Price_ID="171" Description="Race v4" />
    <Store Store_ID="12" Price_ID="162" Description="Doom" />
    <Store Store_ID="12" Price_ID="575" Description="Pac-man" />
    <Store Store_ID="13" Price_ID="167" Description="Demons v3" />
    <Store Store_ID="13" Price_ID="170" Description="Race v4" />
    <Store Store_ID="13" Price_ID="163" Description="Doom" />
    <Store Store_ID="13" Price_ID="576" Description="Pac-man" />
  </Game>
  <Game>
    <Place City="NYC" State="NY" />
    <Place City="ATL" State="GA" />
    <Store Store_ID="12" Price_ID="166" Description="Grand Thft Auto V" />
    <Store Store_ID="12" Price_ID="171" Description="Red Dead Redemption" />
    <Store Store_ID="12" Price_ID="162" Description="Super Mario Brothers 3" />
    <Store Store_ID="12" Price_ID="575" Description="Game Controller" />
    <Store Store_ID="13" Price_ID="167" Description="Grand Thft Auto V" />
    <Store Store_ID="13" Price_ID="170" Description="Red Dead Redemption" />
    <Store Store_ID="13" Price_ID="163" Description="Super Mario Brothers 3" />
    <Store Store_ID="13" Price_ID="576" Description="Game Controller" />
  </Game>
  <Game>
    <Place City="MIA" State="FL" />
    <Place City="LAS" State="NV" />
    <Store Store_ID="12" Price_ID="349" Description="Demons v3" />
    <Store Store_ID="12" Price_ID="350" Description="Race v4" />
    <Store Store_ID="12" Price_ID="346" Description="Doom" />
    <Store Store_ID="12" Price_ID="579" Description="Pac-man" />
    <Store Store_ID="13" Price_ID="167" Description="Grand Theft Auto V" />
    <Store Store_ID="13" Price_ID="170" Description="Red Dead Redemption" />
    <Store Store_ID="13" Price_ID="163" Description="Super Mario Brothers 3" />
    <Store Store_ID="13" Price_ID="576" Description="Game Controller" />
  </Game>
</Games>

I am having difficulty filtering and returning the desired information using SQL.我在使用 SQL 过滤和返回所需信息时遇到困难。 What I'm looking to accomplish is to filter using the <Place> element's attributes: <city> and <state> to return a complete list of <Store> attributes IDs:我想要完成的是使用<Place>元素的属性进行过滤: <city><state>以返回<Store>属性 ID 的完整列表:

For example:例如:

SELECT G.S.Store_ID AS Store
,      G.S.Price_ID AS Price
FROM @xml.nodes('') G(S)
WHERE City="NYC" AND State="NY" 

should return:应该返回:

Store #    Price #
========   ========
12         166
12         171
12         162
12         575 
13         167                   
13         170                   
13         163                                                
13         576   

Please try the following solution.请尝试以下解决方案。

SQL SQL

DECLARE @xml XML =
N'<Games>
    <Game>
        <Place City="LAS" State="NV"/>
        <Place City="ATL" State="GA"/>
        <Store Store_ID="12" Price_ID="166" Description="Demons v3"/>
        <Store Store_ID="12" Price_ID="171" Description="Race v4"/>
        <Store Store_ID="12" Price_ID="162" Description="Doom"/>
        <Store Store_ID="12" Price_ID="575" Description="Pac-man"/>
        <Store Store_ID="13" Price_ID="167" Description="Demons v3"/>
        <Store Store_ID="13" Price_ID="170" Description="Race v4"/>
        <Store Store_ID="13" Price_ID="163" Description="Doom"/>
        <Store Store_ID="13" Price_ID="576" Description="Pac-man"/>
    </Game>
    <Game>
        <Place City="NYC" State="NY"/>
        <Place City="ATL" State="GA"/>
        <Store Store_ID="12" Price_ID="166" Description="Grand Thft Auto V"/>
        <Store Store_ID="12" Price_ID="171" Description="Red Dead Redemption"/>
        <Store Store_ID="12" Price_ID="162" Description="Super Mario Brothers 3"/>
        <Store Store_ID="12" Price_ID="575" Description="Game Controller"/>
        <Store Store_ID="13" Price_ID="167" Description="Grand Thft Auto V"/>
        <Store Store_ID="13" Price_ID="170" Description="Red Dead Redemption"/>
        <Store Store_ID="13" Price_ID="163" Description="Super Mario Brothers 3"/>
        <Store Store_ID="13" Price_ID="576" Description="Game Controller"/>
    </Game>
    <Game>
        <Place City="MIA" State="FL"/>
        <Place City="LAS" State="NV"/>
        <Store Store_ID="12" Price_ID="349" Description="Demons v3"/>
        <Store Store_ID="12" Price_ID="350" Description="Race v4"/>
        <Store Store_ID="12" Price_ID="346" Description="Doom"/>
        <Store Store_ID="12" Price_ID="579" Description="Pac-man"/>
        <Store Store_ID="13" Price_ID="167" Description="Grand Theft Auto V"/>
        <Store Store_ID="13" Price_ID="170" Description="Red Dead Redemption"/>
        <Store Store_ID="13" Price_ID="163" Description="Super Mario Brothers 3"/>
        <Store Store_ID="13" Price_ID="576" Description="Game Controller"/>
    </Game>
</Games>';

SELECT c.value('@Store_ID', 'INT') AS Store_ID
    , c.value('@Price_ID', 'INT') AS Price_ID
FROM @xml.nodes('/Games/Game[Place[@City="NYC" and @State="NY"]]/Store') AS t(c)

Output输出

+----------+----------+
| Store_ID | Price_ID |
+----------+----------+
|       12 |      166 |
|       12 |      171 |
|       12 |      162 |
|       12 |      575 |
|       13 |      167 |
|       13 |      170 |
|       13 |      163 |
|       13 |      576 |
+----------+----------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM