Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 删除重复项并对模拟group_concat的结果进行排序_Sql Server 2005_Tsql_Sorting_Duplicates_Group Concat - Fatal编程技术网

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