我试图针对oracle DB编写查询,我需要从五行中获取一个字段并返回包含所有五个字段的行。例如:
PK RANDOM_COLUMN IMPORTANT_COLUMN
1 Blah 48
2 Blah 02
3 Blah 67
4 Blah 82
5 Blah 99
6 Blah 10
7 Blah 21
我想从中选择并获取此信息:
RANDOM_COLUMN IPC1 IPC2 IPC3 IPC4 IPC5
Blah 48 02 67 82 99
Blah 10 21
RANDOM_COLUMN实际上是多个列,它们都包含相同的数据。知道如何才能得到我正在寻找的结果吗?
更新:删除,因为它太具体了。
答案 0 :(得分:1)
您可以在使用row_number()
为每行分配序号后手动转动数据。需要一些额外的算术:
select blah,
max(case when mod(seqnum, 5) = 1 then important_column end) as ipc1,
max(case when mod(seqnum, 5) = 2 then important_column end) as ipc2,
max(case when mod(seqnum, 5) = 3 then important_column end) as ipc3,
max(case when mod(seqnum, 5) = 4 then important_column end) as ipc4,
max(case when mod(seqnum, 5) = 0 then important_column end) as ipc5
from (select t.*, row_number() over (partition by blah order by pk) as seqnum
from t
) t
group by blah, trunc((seqnum - 1) / 5);