数据转置在表格中

时间:2015-07-26 11:54:08

标签: sql oracle transpose

我有下表:

  type          sw_rel          id         oper_name          test_name
test on a        1.0           1478       first_stage      initial stage
test on b        1.0           1478       first_stage      initial stage
test on c        1.0           1478       first_stage      initial stage
at stage 1       2.0           1510       first_stage      initial stage
at stage 1       2.0           1511       first_stage      initial stage
at stage 1       2.0           1512       first_stage      initial stage
at stage 2       2.0           1513       first_stage      initial stage
at stage 2       2.0           1514       first_stage      initial stage
at stage 2       2.0           1515       first_stage      initial stage

我想获得以下输出。但是无法得到。

test on a,1.0,first_stage,initial stage,1478
test on b,1.0,first_stage,initial stage,1478
test on c,1.0,first_stage,initial stage,1478
at stage 1,2.0,first_stage,initial stage,1510,1511,1512
at stage 2,2.0,first_stage,initial stage,1513,1514,1515

在这里,我正在尝试编写Oracle SQL转置查询。

主要逻辑是 具有相同(type,sw_rel,oper_name,test_name)的行应该以{{1​​}}行(逗号分隔)。

1 个答案:

答案 0 :(得分:0)

我认为你可以使用条件聚合做你想做的事情:

select type, sw_rel, oper_name, test_name,
       list_agg(id, ',') within group (order by id) as ids
from table t
group by type, sw_rel, oper_name, test_name;