﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / XML query / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 20:53:35 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: XML query</title><link>http://www.sqlservercentral.com/Forums/Topic1410042-392-1.aspx</link><description>Maybe there's a middlin' easy alternative:[code="sql"]DECLARE @xmlData XML ='&amp;lt;MatchKeys&amp;gt;    &amp;lt;fuzzyKeys&amp;gt;      &amp;lt;key key1="MatchKey_PostOut" key2="MatchKey_Name1" /&amp;gt;      &amp;lt;key key1="MatchKey_Name1" key2="MatchKey_PhoneticStreet" key3="MatchKey_PhoneticCompanyName"/&amp;gt;      &amp;lt;key key1="MatchKey_PostOut" key2="MatchKey_PostIn" /&amp;gt;    &amp;lt;/fuzzyKeys&amp;gt;  &amp;lt;/MatchKeys&amp;gt;';;WITH XML_Unveiled AS (    SELECT MatchKey_Group=ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),      x.ref.value('@key1[1]', 'varchar(50)') AS Key1,      x.ref.value('@key2[1]', 'varchar(50)') AS Key2,      x.ref.value('@key3[1]', 'varchar(50)') AS Key3    FROM @xmlData.nodes('/MatchKeys/fuzzyKeys/key') AS x( ref ))SELECT MatchKey_Group, MatchKey_ID, KeyColumnFROM XML_UnveiledCROSS APPLY (    VALUES (1, Key1),(2,Key2),(3,Key3)) a (MatchKey_ID, KeyColumn)WHERE KeyColumn IS NOT NULL ORDER BY MatchKey_Group, MatchKey_ID[/code]See the first link in my signature if you haven't seen the CROSS APPLY VALUES approach to UNPIVOT before.</description><pubDate>Tue, 22 Jan 2013 21:30:27 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: XML query</title><link>http://www.sqlservercentral.com/Forums/Topic1410042-392-1.aspx</link><description>Thanks guys. Much appreciated!Although I find Greg's solution easier to understand which makes me wonder, should I re-format my XML?!BTW, I've no idea how Mark's solution works lol, I'm going to have spend some time to work it out.Once again, thanks for your help!</description><pubDate>Tue, 22 Jan 2013 09:16:29 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: XML query</title><link>http://www.sqlservercentral.com/Forums/Topic1410042-392-1.aspx</link><description>[quote][b]Greg Snidow[/b]Mark, what is the purpose of the DENSE_RANK(), ROW_NUMBER(), and the CROSS APPLY?  This is the fist time I've messed with XML, so it may be something simple.[/quote]Okay, I see now.  Abu does not have the ID columns in the XML, so you are creating them?Abu, I think you can ignore my previous post.</description><pubDate>Tue, 22 Jan 2013 09:04:34 GMT</pubDate><dc:creator>Greg Snidow</dc:creator></item><item><title>RE: XML query</title><link>http://www.sqlservercentral.com/Forums/Topic1410042-392-1.aspx</link><description>Well, looks like Mark beat me to it.  Anyhow, this is kind of (I think) the same thing.[code]DECLARE @xmlData XMLSET @xmlData = ('&amp;lt;MatchKeys&amp;gt;  &amp;lt;fuzzyKeys&amp;gt;    &amp;lt;key key1="1" key2="1" key3="MatchKey_PostOut" /&amp;gt;  &amp;lt;/fuzzyKeys&amp;gt;  &amp;lt;fuzzyKeys&amp;gt;    &amp;lt;key key1="1" key2="2" key3="MatchKey_Name1" /&amp;gt;  &amp;lt;/fuzzyKeys&amp;gt;  &amp;lt;fuzzyKeys&amp;gt;    &amp;lt;key key1="2" key2="1" key3="MatchKey_Name1" /&amp;gt;  &amp;lt;/fuzzyKeys&amp;gt;  &amp;lt;fuzzyKeys&amp;gt;    &amp;lt;key key1="2" key2="2" key3="MatchKey_PhoneticStreet" /&amp;gt;  &amp;lt;/fuzzyKeys&amp;gt;  &amp;lt;fuzzyKeys&amp;gt;    &amp;lt;key key1="2" key2="3" key3="MatchKey_PhoneticCompanyName" /&amp;gt;  &amp;lt;/fuzzyKeys&amp;gt;  &amp;lt;fuzzyKeys&amp;gt;    &amp;lt;key key1="3" key2="1" key3="MatchKey_PostOut" /&amp;gt;  &amp;lt;/fuzzyKeys&amp;gt;  &amp;lt;fuzzyKeys&amp;gt;    &amp;lt;key key1="3" key2="2" key3="MatchKey_PostIn" /&amp;gt;  &amp;lt;/fuzzyKeys&amp;gt;&amp;lt;/MatchKeys&amp;gt;')SELECT   x.ref.value('@key1[1]', 'int') AS MatchKey_Group,  x.ref.value('@key2[1]', 'int') AS MatchKey_ID,  x.ref.value('@key3[1]', 'varchar(50)') AS KeyColumnFROM @xmlData.nodes('/MatchKeys/fuzzyKeys/key') AS x( ref )[/code]Mark, what is the purpose of the DENSE_RANK(), ROW_NUMBER(), and the CROSS APPLY?  This is the fist time I've messed with XML, so it may be something simple.</description><pubDate>Tue, 22 Jan 2013 09:01:03 GMT</pubDate><dc:creator>Greg Snidow</dc:creator></item><item><title>RE: XML query</title><link>http://www.sqlservercentral.com/Forums/Topic1410042-392-1.aspx</link><description>[code="sql"]DECLARE @x XML ='&amp;lt;MatchKeys&amp;gt;    &amp;lt;fuzzyKeys&amp;gt;      &amp;lt;key key1="MatchKey_PostOut" key2="MatchKey_Name1" /&amp;gt;      &amp;lt;key key1="MatchKey_Name1" key2="MatchKey_PhoneticStreet" key3="MatchKey_PhoneticCompanyName"/&amp;gt;      &amp;lt;key key1="MatchKey_PostOut" key2="MatchKey_PostIn" /&amp;gt;    &amp;lt;/fuzzyKeys&amp;gt;  &amp;lt;/MatchKeys&amp;gt;';SELECT DENSE_RANK() OVER(ORDER BY x1.r1) AS MatchKey_Group,       ROW_NUMBER() OVER(PARTITION BY x1.r1 ORDER BY x2.r2) AS MatchKey_ID,       x2.r2.value('.','VARCHAR(30)') AS KeyColumnFROM @x.nodes('/MatchKeys/fuzzyKeys/key') AS x1(r1)CROSS APPLY x1.r1.nodes('@*') AS x2(r2);[/code]</description><pubDate>Tue, 22 Jan 2013 08:52:25 GMT</pubDate><dc:creator>Mark-101232</dc:creator></item><item><title>XML query</title><link>http://www.sqlservercentral.com/Forums/Topic1410042-392-1.aspx</link><description>I have the following basic XML document:[code="xml"]&amp;lt;MatchKeys&amp;gt;    &amp;lt;fuzzyKeys&amp;gt;      &amp;lt;key key1="MatchKey_PostOut" key2="MatchKey_Name1" /&amp;gt;      &amp;lt;key key1="MatchKey_Name1" key2="MatchKey_PhoneticStreet" key3="MatchKey_PhoneticCompanyName"/&amp;gt;      &amp;lt;key key1="MatchKey_PostOut" key2="MatchKey_PostIn" /&amp;gt;    &amp;lt;/fuzzyKeys&amp;gt;  &amp;lt;/MatchKeys&amp;gt;[/code]The data is stored in an XML SQL variable but now I would like to put it into a table so the data looks like this:[code="sql"]with cteMatchKeys (MatchKey_Group, MatchKey_ID, KeyColumn)as(select 1, 1, 'MatchKey_PostOut' union allselect 1, 2, 'MatchKey_Name1' union allselect 2, 1, 'MatchKey_Name1' union allselect 2, 2, 'MatchKey_PhoneticStreet' union allselect 2, 3, 'MatchKey_PhoneticCompanyName' union allselect 3, 1, 'MatchKey_PostOut' union allselect 3, 2, 'MatchKey_PostIn')select * from cteMatchKeys[/code]Any ideas?</description><pubDate>Tue, 22 Jan 2013 07:43:15 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item></channel></rss>