在一行中显示一列的多个值 (SQL Oracle)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8508078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:30:44  来源:igfitidea点击:

Display multiple values of a column in one row (SQL Oracle)

sqloracle

提问by BvilleBullet

Possible Duplicate:
Is there an Oracle SQL query that aggregates multiple rows into one row?

可能的重复:
是否有将多行聚合为一行的 Oracle SQL 查询?

I'm trying to to create a SQL query that can return data from a table to display multiple values of a column in one row.

我正在尝试创建一个 SQL 查询,该查询可以从表中返回数据以在一行中显示一列的多个值。

For example this is the table setup:

例如,这是表设置:

SEQ   ROWSEQNUM   ASSISTING_ASSOCIATES  
100   2           19332816  
100   1           1366344  
103   1           12228238  
104   1           1366474 

I need to query results to look like this:

我需要查询结果如下所示:

 SEQ   ROWSEQNUM   ASSISTING_ASSOCIATES  
 100   1           1366344; 19332816  
 103   1           12228238  
 104   1           1366474 

Does anybody have any insight?

有人有任何见解吗?

采纳答案by Dave Costa

I think this ought to work, assuming that for each SEQvalue there is always a row with ROWSEQNUM=1and the values for ROWSEQNUMincrease sequentially with no gaps.

我认为这应该有效,假设对于每个SEQ值总是有一排,ROWSEQNUM=1并且值ROWSEQNUM按顺序增加而没有间隙。

select seq, min(rowseqnum), max(assoc_list)
from (
  select seq, rowseqnum, sys_connect_by_path(assisting_associate,';') assoc_list
    from assoc_table
    start with rowseqnum=1
    connect by seq = prior seq and rowseqnum = prior rowseqnum + 1
  )
group by seq