<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Uniquely Identifying XML Nodes with DENSE_RANK</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/08/03/uniquely-identifying-xml-nodes-with-dense-rank.aspx</link><description>When working with XML in SQL Server, you might want to uniquely identify one node against another. But due to the flexibility with which XML can be defined, this is not always directly possible. SQL Server's own XML structures are guilty of having this</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Uniquely Identifying XML Nodes with DENSE_RANK</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/08/03/uniquely-identifying-xml-nodes-with-dense-rank.aspx#15777</link><pubDate>Wed, 05 Aug 2009 01:05:47 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:15777</guid><dc:creator>Alejandro Mesa</dc:creator><description>&lt;p&gt;Adam,&lt;/p&gt;
&lt;p&gt;Clever solution, indeed.&lt;/p&gt;
&lt;p&gt;I can see in the execution plan, that the segmentation and sequence is done using an id returned by the xml reader.&lt;/p&gt;
&lt;p&gt;Here is another way to accomplish this, but is not as good as the one from Mladen. It uses the all time favorite auxiliary table of numbers, and also relies on counting the parent nodes.&lt;/p&gt;
&lt;p&gt;declare @Numbers table (&lt;/p&gt;
&lt;p&gt;Number int not null unique&lt;/p&gt;
&lt;p&gt;);&lt;/p&gt;
&lt;p&gt;insert into&lt;/p&gt;
&lt;p&gt;	@Numbers(Number)&lt;/p&gt;
&lt;p&gt;Values&lt;/p&gt;
&lt;p&gt;	(1),&lt;/p&gt;
&lt;p&gt;	(2),&lt;/p&gt;
&lt;p&gt;	(3),&lt;/p&gt;
&lt;p&gt;	(4),&lt;/p&gt;
&lt;p&gt;	(5);&lt;/p&gt;
&lt;p&gt;declare @x xml;&lt;/p&gt;
&lt;p&gt;set @x = '&amp;lt;a&amp;gt;&amp;lt;b&amp;gt;&amp;lt;c&amp;gt;abc&amp;lt;/c&amp;gt;&amp;lt;c&amp;gt;def&amp;lt;/c&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;b&amp;gt;&amp;lt;c&amp;gt;abc&amp;lt;/c&amp;gt;&amp;lt;c&amp;gt;def&amp;lt;/c&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;/a&amp;gt;';&lt;/p&gt;
&lt;p&gt;select&lt;/p&gt;
&lt;p&gt;	T.c.value('(./text())[1]', 'varchar(25)') as c,&lt;/p&gt;
&lt;p&gt;	N.Number&lt;/p&gt;
&lt;p&gt;from&lt;/p&gt;
&lt;p&gt;	@Numbers as N&lt;/p&gt;
&lt;p&gt;	cross apply&lt;/p&gt;
&lt;p&gt;	@x.nodes('/a/b[position() = sql:column(&amp;quot;N.Number&amp;quot;)]/.') as X(n)&lt;/p&gt;
&lt;p&gt;	cross apply&lt;/p&gt;
&lt;p&gt;	X.n.nodes('./c') as T(c)&lt;/p&gt;
&lt;p&gt;where&lt;/p&gt;
&lt;p&gt;	N.Number &amp;lt;= @x.query('count(/a/b)').value('.', 'int');&lt;/p&gt;
&lt;p&gt;GO&lt;/p&gt;
&lt;p&gt;All this could be avoided if we were able to have access to the position or even the id of the node. BTW, here is connect entry in case you feel like it deserves one more vote.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=383888&amp;amp;wa=wsignin1.0"&gt;https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=383888&amp;amp;wa=wsignin1.0&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;AMB&lt;/p&gt;
</description></item><item><title>re: Uniquely Identifying XML Nodes with DENSE_RANK</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/08/03/uniquely-identifying-xml-nodes-with-dense-rank.aspx#15780</link><pubDate>Wed, 05 Aug 2009 02:54:31 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:15780</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi Alejandro,&lt;/p&gt;
&lt;p&gt;Voted!&lt;/p&gt;
</description></item><item><title>re: Uniquely Identifying XML Nodes with DENSE_RANK</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/08/03/uniquely-identifying-xml-nodes-with-dense-rank.aspx#32097</link><pubDate>Wed, 29 Dec 2010 16:54:13 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:32097</guid><dc:creator>Matthew Darwin</dc:creator><description>&lt;p&gt;Thanks very much for this, an excellent solution that's really helped massively.&lt;/p&gt;
</description></item><item><title>re: Uniquely Identifying XML Nodes with DENSE_RANK</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/08/03/uniquely-identifying-xml-nodes-with-dense-rank.aspx#34307</link><pubDate>Tue, 22 Mar 2011 13:58:48 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34307</guid><dc:creator>Maciej Raszplewicz</dc:creator><description>&lt;p&gt;Adam,&lt;/p&gt;
&lt;p&gt;Is result order of this query with dense_rank always the same? I know that select is non-deterministic but what if I select data from xml?&lt;/p&gt;
&lt;p&gt;Maciek&lt;/p&gt;
</description></item><item><title>re: Uniquely Identifying XML Nodes with DENSE_RANK</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/08/03/uniquely-identifying-xml-nodes-with-dense-rank.aspx#34317</link><pubDate>Tue, 22 Mar 2011 15:14:59 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34317</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Maciej,&lt;/p&gt;
&lt;p&gt;No, order is never guaranteed unless you use ORDER BY -- XML or no. Please use ORDER BY :-)&lt;/p&gt;
</description></item><item><title>re: Uniquely Identifying XML Nodes with DENSE_RANK</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/08/03/uniquely-identifying-xml-nodes-with-dense-rank.aspx#34319</link><pubDate>Tue, 22 Mar 2011 15:24:46 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34319</guid><dc:creator>Maciej Raszplewicz</dc:creator><description>&lt;p&gt;I mean order of dense_rank. There is ORDER BY b_node there.&lt;/p&gt;
&lt;p&gt;My first question was not exactly what I meant.&lt;/p&gt;
</description></item><item><title>re: Uniquely Identifying XML Nodes with DENSE_RANK</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/08/03/uniquely-identifying-xml-nodes-with-dense-rank.aspx#48450</link><pubDate>Thu, 28 Mar 2013 16:29:13 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:48450</guid><dc:creator>Serg Yurevich</dc:creator><description>&lt;p&gt;This is very elegant solution. I like it very much and it helped me much. Genius!&lt;/p&gt;
</description></item></channel></rss>