SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Xquery Problem


Xquery Problem

Author
Message
s_osborne2
s_osborne2
SSC Eights!
SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)

Group: General Forum Members
Points: 997 Visits: 2292
Hi,

I have a field that contains some XML. The root level is ENTITIES there might be many ENTITY nodes. Here is an example query.

DECLARE @table TABLE
(
RiskXML XML
)

INSERT INTO @table
(
[RiskXML]
)
VALUES
(
'<ENTITIES>
<ENTITY Id="10863" Type="Properties">
<GROUP Type="No_Group">
<RISKGROUP Id="11251" Type="No_Group">
<RISKKEYITEM Name="donotshow_EntityGroupRepeatIndex" Value="1" Type="Integer" />
</RISKGROUP>
</GROUP>
</ENTITY>
<ENTITY Id="10864" Type="Properties">
<GROUP Type="No_Group">
<RISKGROUP Id="11252" Type="No_Group">
<RISKKEYITEM Name="donotshow_EntityGroupRepeatIndex" Value="2" Type="Integer" />
</RISKGROUP>
</GROUP>
</ENTITY>
<ENTITY Id="10865" Type="Properties">
<GROUP Type="No_Group">
<RISKGROUP Id="11253" Type="No_Group">
<RISKKEYITEM Name="donotshow_EntityGroupRepeatIndex" Value="3" Type="Integer" />
</RISKGROUP>
</GROUP>
</ENTITY>
</ENTITIES>'
)


SELECT
ROW_NUMBER() OVER (ORDER BY e.value('(./@Id)','int')) AS EntityNumber,
e.value('(./@Id)','int') AS EntityId,
e.query('(//ENTITY/GROUP/RISKGROUP/RISKKEYITEM[@Name="donotshow_EntityGroupRepeatIndex"])') AS EntityRKI,
t.RiskXML
FROM
@table t
CROSS APPLY t.RiskXML.nodes('//ENTITIES/ENTITY') Entities(e)



So... looking at that result set in the EntityRKI column there are 3 RISKKEYITEM nodes (one for each entity). What i want is the RISKKEYITEM node for the EntityId, not all 3 RISKKEYITEM nodes for each EntityId. Does anyone know how i can do that?

I appreciate my description may not be great so feel free to ask any questions and i'll try to clarify any confusion...

Thanks,


Simon



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
stuart-leaf-node
stuart-leaf-node
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 142
You need to 'anchor' the query to the entity as well:


SELECT
ROW_NUMBER() OVER (ORDER BY e.value('(./@Id)','int')) AS EntityNumber,
e.value('(./@Id)','int') AS EntityId,
e.query('(./GROUP/RISKGROUP/RISKKEYITEM[@Name="donotshow_EntityGroupRepeatIndex"])') AS EntityRKI,
t.RiskXML
FROM
@table t
CROSS APPLY t.RiskXML.nodes('//ENTITIES/ENTITY') Entities(e)



That returns what I think you're looking for.

blog | napalmgram@Twitter


Training cats makes SQL Server look easy

s_osborne2
s_osborne2
SSC Eights!
SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)

Group: General Forum Members
Points: 997 Visits: 2292
That's exactly what I wanted. Nice one.



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search