[英]Concatenate rows to single row in SQL Server 2008
我使用 MonetDb 進行分析,它使用 SQL Server 2008 進行查詢處理。
我有一個表 ROLES,其中包含以下數據。
Emp ROLE
1 ROLE_1
1 ROLE_2
1 ROLE_3
2 ROLE_1
2 ROLE_2
3 ROLE_3
3 ROLE_4
我想輸出為
EMP ROLE
1 ROLE_1, ROLE_2, ROLE_3
2 ROLE_1, ROLE_2
3 ROLE_3, ROLE_4
我試過 group_concat,但它說
沒有這樣的聚合“group_concat”
我試過的查詢如下
select
emp,
group_concat(role) as wo
from
roles
group by emp
group_concat
有沒有其他選擇?
編輯:
請閱讀評論,我使用的是不支持 group_concat 和 xml 的 monetdb。
Monetdb 具有 R 集成。 你可以寫一個R函數。
R函數:
CREATE AGGREGATE str_aggre(val STRING) RETURNS STRING LANGUAGE R {
aggregate(val, by=list(aggr_group), FUN=toString)$x
};
SQL 命令:
select
emp,
str_aggre(role) as wo
from
roles
group by emp
這將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.