Sql server 2005 删除重复项并对模拟group_concat的结果进行排序
我已经找到了这个有趣的线索 这给了我Sql server 2005 删除重复项并对模拟group_concat的结果进行排序,sql-server-2005,tsql,sorting,duplicates,group-concat,Sql Server 2005,Tsql,Sorting,Duplicates,Group Concat,我已经找到了这个有趣的线索 这给了我 1 5,2,5 2 1 3 5,2,2 4 5,3 但是,我想从每次考试中删除重复项,并对连接的结果进行排序,以便 1 2,5 2 1 3 2,5 4 3,5 谢谢。尝试在内部查询中使用DISTINCT,并按方法排序,而不是使用id\u检查 select id_exam, method = replace ((select distinct method AS [data()] from
1 5,2,5
2 1
3 5,2,2
4 5,3
但是,我想从每次考试中删除重复项,并对连接的结果进行排序,以便
1 2,5
2 1
3 2,5
4 3,5
谢谢。尝试在内部查询中使用DISTINCT,并按方法排序,而不是使用id\u检查
select
id_exam,
method = replace ((select distinct method AS [data()]
from methods
where id_exam = a.id_exam
order by method for xml path('')), ' ', ',')
from methods a
where id_exam is not null
group by id_exam
尝试在内部查询和排序方法中使用DISTINCT而不是id\u检查
select
id_exam,
method = replace ((select distinct method AS [data()]
from methods
where id_exam = a.id_exam
order by method for xml path('')), ' ', ',')
from methods a
where id_exam is not null
group by id_exam
将
按方法分组
添加到内部查询中,并将按id排序检查
更改为按方法排序
select
id_exam,
method = replace ((select method AS [data()]
from methods
where id_exam = a.id_exam
group by method
order by method for xml path('')), ' ', ',')
from methods a
where id_exam is not null
group by id_exam
结果:
id_exam method
----------- ---------
1 2,5
2 1
3 2,5
4 3,5
将
按方法分组
添加到内部查询中,并将按id排序检查
更改为按方法排序
select
id_exam,
method = replace ((select method AS [data()]
from methods
where id_exam = a.id_exam
group by method
order by method for xml path('')), ' ', ',')
from methods a
where id_exam is not null
group by id_exam
结果:
id_exam method
----------- ---------
1 2,5
2 1
3 2,5
4 3,5