简体   繁体   English

将行连接到 SQL Server 2008 中的单行

[英]Concatenate rows to single row in SQL Server 2008

I'm using MonetDb for analytics, which is using SQL Server 2008 for query processing.我使用 MonetDb 进行分析,它使用 SQL Server 2008 进行查询处理。

I have a table ROLES, which has data below.我有一个表 ROLES,其中包含以下数据。

Emp ROLE
1   ROLE_1
1   ROLE_2
1   ROLE_3
2   ROLE_1
2   ROLE_2
3   ROLE_3
3   ROLE_4

I want output as我想输出为

EMP ROLE
1   ROLE_1, ROLE_2, ROLE_3
2   ROLE_1, ROLE_2
3   ROLE_3, ROLE_4

I tried group_concat, but it is saying我试过 group_concat,但它说

no such aggregate 'group_concat'没有这样的聚合“group_concat”

Query I tried is below我试过的查询如下

select
    emp,
    group_concat(role) as wo
from 
    roles 
group by emp

Is there any alternative for group_concat ? group_concat有没有其他选择?

Edit:编辑:

Please read comments, I'm using monetdb which doesn't support group_concat and xml.请阅读评论,我使用的是不支持 group_concat 和 xml 的 monetdb。

Monetdb has R integration. Monetdb 具有 R 集成。 You can write a R function.你可以写一个R函数。

Check this link检查此链接

R function: R函数:

CREATE AGGREGATE str_aggre(val STRING) RETURNS STRING LANGUAGE R {
   aggregate(val, by=list(aggr_group), FUN=toString)$x 
};

SQL Command: SQL 命令:

select
    emp,
    str_aggre(role) as wo
from 
    roles 
group by emp

This will work.这将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM