Sql GROUPBY与XML的结合

Sql GROUPBY与XML的结合,sql,sql-server-2008,Sql,Sql Server 2008,我有一个类似的表(当然,这只是示例内容): ID xml一些其他值甚至更多值。。。。 ------------------------------------------------------------------------------- 指南1 指南12 指南1 指南2 3 4 当其中一个应该分组的字段包含xml内容时,是否可以按ID分组?这些行中可能存在重复项,或者列值可以为null,但它们当然不会包含相同ID的不同值 我希望桌子看起来像这样: ID xml

我有一个类似的表(当然,这只是示例内容):

ID xml一些其他值甚至更多值。。。。
-------------------------------------------------------------------------------
指南1
指南12
指南1
指南2 3 4
当其中一个应该分组的字段包含xml内容时,是否可以按ID分组?这些行中可能存在重复项,或者列值可以为null,但它们当然不会包含相同ID的不同值

我希望桌子看起来像这样:

ID           xml          some_other_value      even_more_values            ....
--------------------------------------------------------------------------------
GUID1        <A />                      1                     2
GUID2        <B />                      3                     4
ID xml一些其他值甚至更多值。。。。
--------------------------------------------------------------------------------
指南1 2
指南2 3 4

有什么想法吗?

您可以使用
行号()over()


您可以将xml转换为varchar,并按此进行分组

 group by convert(varchar(max),xml)          
你可以这样试试。 将xml转换为varchar

select distinct t.id,t.xml,t1.first t1,t2.second t2
from test t
inner join test t1 on t1.id=t.id and t1.first is not null
inner join test t2 on t2.id=t.id and t2.second is not null;

可能类似于:

select * 
from
    (select 
      id
      ,max(isnull(someothervalue,0)) as someothervalue
      ....
    from table
    group by id) si
cross apply (select top 1 xml from table where id = si.id) xmli(xml)
select distinct t.id,t.xml,t1.first t1,t2.second t2
from test t
inner join test t1 on t1.id=t.id and t1.first is not null
inner join test t2 on t2.id=t.id and t2.second is not null;
select * 
from
    (select 
      id
      ,max(isnull(someothervalue,0)) as someothervalue
      ....
    from table
    group by id) si
cross apply (select top 1 xml from table where id = si.id) xmli(xml)