我有如下表。我想将列连接为 all_cells,例如第一个网格的 ABC27_0.5,XYZ22_0.7,DDD_0.99 和第二个网格的 FFF33_0.7 。我怎样才能获得这样的数百万条记录。
grid top1_cell top1_ratio top2_cell top2_ratio top3_cell top3_ratio
666666666666666666 ABC27 0.5 XYZ22 0.7 DDD 0.99
77777777777777777 FFF33 0.7
我尝试了下面的代码,但我不想包含空列。例如,对于第二个网格,我不需要像
FFF33_0.7,_,_
这样的东西,我只想要 FFF33_0.7
。我怎样才能做到这一点?
SELECT X.*,
RTRIM(XMLAGG(XMLELEMENT(E,TOP_1_CELL||'_'||TOP_1_RATIO,',',TOP_2_CELL||'_'||TOP_2_RATIO,',',TOP_3_CELL||'_'||TOP_3_RATIO).EXTRACT('//text()') ORDER BY TOP_1_CELL,TOP_2_CELL,TOP_3_CELL ).GetClobVal(),',') AS all_Cells
FROM X
GROUP BY grid
纯串联然后美化怎么样?
样本数据:
SQL> select * From test;
GRID TOP1_CELL TOP1_RATIO TOP2_CELL TOP2_RATIO TOP3_CELL TOP3_RATIO
---------- ---------- ---------- ---------- ---------- ---------- ----------
666 ABC27 .5 XYZ22 .7 DDD .99
777 FFF33 .7
888 DEF13 .2
查询:连接值,删除多余的逗号:
SQL> with temp as
2 (
3 select grid,
4 top1_cell || case when top1_ratio is not null then '_' || to_char(top1_ratio, 'fm0.0') end ||','||
5 top2_cell || case when top2_ratio is not null then '_' || to_char(top2_ratio, 'fm0.0') end ||','||
6 top3_cell || case when top3_ratio is not null then '_' || to_char(top3_ratio, 'fm0.0') end result
7 from test
8 )
9 select grid,
10 trim(both ',' from result) as result
11 from temp;
GRID RESULT
---------- ------------------------------
666 ABC27_0.5,XYZ22_0.7,DDD_1.0
777 FFF33_0.7
888 DEF13_0.2
SQL>