Oracle 8i 至少支持分析函数,因此您可以为每个颜色值分配一个标称数字,row_number()
或dense_rank()
(按对您有意义的任何方式排序):
select name, color,
row_number() over (partition by name order by color) as rn
from your_table;
然后在手动枢轴上使用一个变体,max(decode())
每个可能的行号一个:
select name,
max(decode(rn, 1, color))
|| max(decode(rn, 2, color))
|| max(decode(rn, 3, color))
|| max(decode(rn, 4, color))
|| max(decode(rn, 5, color))
|| max(decode(rn, 6, color))
|| max(decode(rn, 7, color))
-- ...
as colors
from (
select name, color,
row_number() over (partition by name order by color) as rn
from your_table
)
group by name
order by name;
NAME COLORS
---------- ----------------------------------------------------------------------
Bob BlueRed
John PurpleRedYellow
Tom Green
您说范围是未知的,但即使每个名称的行数不受限制,您仍然受到最终连接字符串最多 4000 个字符的限制 - 根据您对实际颜色值的了解你的最大可用数量。(您可以一次性自动生成解码部分)。
如果需要,您可以以相同的方式包含分隔符:
select name,
max(decode(rn, 1, color))
|| max(decode(rn, 2, ',')) || max(decode(rn, 2, color))
|| max(decode(rn, 3, ',')) || max(decode(rn, 3, color))
|| max(decode(rn, 4, ',')) || max(decode(rn, 4, color))
|| max(decode(rn, 5, ',')) || max(decode(rn, 5, color))
|| max(decode(rn, 6, ',')) || max(decode(rn, 6, color))
|| max(decode(rn, 7, ',')) || max(decode(rn, 7, color))
|| max(decode(rn, 8, ',')) || max(decode(rn, 8, color))
-- ...
as colors
from (
select name, color,
row_number() over (partition by name order by color) as rn
from your_table
where color is not null
)
group by name
order by name;
NAME COLORS
---------- ---------------------------------------------------------------------------------------
Bob Blue,Red
John Purple,Red,Yellow
Tom Green
我is not null
在内部查询中包含了一个过滤器,以防该列可以为空 - 这应该防止最终列表中出现空元素。
(未在 8i 中测试,因为我找不到可以启动的旧实例,但我认为这没有使用稍后介绍的任何内容......)