Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/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 如何将行连接成单个文本字符串?_Sql_Oracle_Function_Select - Fatal编程技术网

Sql 如何将行连接成单个文本字符串?

Sql 如何将行连接成单个文本字符串?,sql,oracle,function,select,Sql,Oracle,Function,Select,这是我在StackOverflow中的第一个问题,我希望您能给我一点帮助 我需要一个查询,该查询返回一个字符串,其中所有用户的分数都按用户名分组。我将尝试举例说明: username score John421 4028 Robin 5392 Ninaww 2234 Robin 4631 Robin 2792 John421

这是我在StackOverflow中的第一个问题,我希望您能给我一点帮助

我需要一个查询,该查询返回一个字符串,其中所有用户的分数都按用户名分组。我将尝试举例说明:

username          score
John421             4028
Robin                  5392
Ninaww              2234
Robin                  4631
Robin                  2792
John421             8924
查询应返回:

username          all_scores
John421            '4028,8924'
Robin                 '5392,4631,2792'
Ninaww             '2234'
希望有人能帮助我,并提前表示感谢!:)

使用oracle Listag()聚合函数,如下所述:

比如说,

select username, listagg(score, ',') within group (order by score) as all_scores
from mytable
group by username

这里应该使用listag函数。可以使用此函数的分析版本或组集版本

由于需要按用户名汇总结果,因此组集版本似乎是正确的选择

-识别组(这里是用户名)

-识别delimeter“;”

-确定分数的排序。e、 g.得分)


使用
listagg
函数。欢迎使用堆栈溢出。你应该看看,因为人们通常期望人们在自己身上做一些工作,并在问题中解释他们做了什么以及他们的努力是如何失败的。幸运的是,今天人们都为你感到难过,你有了一些答案!
SELECT username,
       LISTAGG(score, '; ') WITHIN GROUP (ORDER BY score) all_scores 
  FROM user_scores 
  GROUP BY username 
  ORDER BY username;