删除重复项并模拟 group_concat 对结果进行排序
我已经找到了这个有趣的线程
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在内部查询中使用 DISTINCT 并按方法排序而不是 id_exam。
Try using DISTINCT in the inner query and ordering by method instead of id_exam.
将
group by method
添加到内部查询,并将order by id_exam
更改为order by method
。结果:
Add a
group by method
to the inner query and changeorder by id_exam
toorder by method
.Result: