删除重复项并模拟 group_concat 对结果进行排序

发布于 2024-11-16 15:16:54 字数 1230 浏览 0 评论 0原文

我已经找到了这个有趣的线程

Simizing group_concat MySQL function在 Microsoft SQL Server 2005 中?

use test
go
create table methods (
id int identity,
id_exam int,
method int
)
go
insert into methods (id_exam,method) values (1,5)
insert into methods (id_exam,method) values (1,2)
insert into methods (id_exam,method) values (1,5)
insert into methods (id_exam,method) values (2,1)
insert into methods (id_exam,method) values (3,5)
insert into methods (id_exam,method) values (3,2)
insert into methods (id_exam,method) values (3,2)
insert into methods (id_exam,method) values (4,5)
insert into methods (id_exam,method) values (4,3)

select 
id_exam, 
method = replace ((select method AS [data()]
                   from methods
                   where id_exam = a.id_exam                      
                   order by id_exam for xml path('')), ' ', ',')
from  methods a
where id_exam is not null
group by id_exam

这给了我

1   5,2,5
2   1
3   5,2,2
4   5,3

但是我想从每次考试中删除重复项并对串联结果进行排序以获得

1  2,5
2  1
3  2,5
4  3,5

感谢。

I've already found this interesting thread

Simulating group_concat MySQL function in Microsoft SQL Server 2005?

use test
go
create table methods (
id int identity,
id_exam int,
method int
)
go
insert into methods (id_exam,method) values (1,5)
insert into methods (id_exam,method) values (1,2)
insert into methods (id_exam,method) values (1,5)
insert into methods (id_exam,method) values (2,1)
insert into methods (id_exam,method) values (3,5)
insert into methods (id_exam,method) values (3,2)
insert into methods (id_exam,method) values (3,2)
insert into methods (id_exam,method) values (4,5)
insert into methods (id_exam,method) values (4,3)

select 
id_exam, 
method = replace ((select method AS [data()]
                   from methods
                   where id_exam = a.id_exam                      
                   order by id_exam for xml path('')), ' ', ',')
from  methods a
where id_exam is not null
group by id_exam

that gives me

1   5,2,5
2   1
3   5,2,2
4   5,3

However I'd like to remove duplicates from each exam and sorting the concatenated results in order to get

1  2,5
2  1
3  2,5
4  3,5

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白日梦 2024-11-23 15:16:54

尝试在内部查询中使用 DISTINCT 并按方法排序而不是 id_exam。

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

Try using DISTINCT in the inner query and ordering by method instead of id_exam.

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
迷途知返 2024-11-23 15:16:54

group by method 添加到内部查询,并将 order by id_exam 更改为 order by method

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

Add a group by method to the inner query and change order by id_exam to order by method.

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

Result:

id_exam     method
----------- ---------
1           2,5
2           1
3           2,5
4           3,5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文