select s_info_windcode, rate
FROM OptionEmbeddedBondRate
where s_info_windcode ='110031.SH'
查询表中股票代码为110031.SH的数据:
现在要做的是通过listagg() WITHIN GROUP () 函数,将后面的rate合并为一行:
SELECT
s_info_windcode,
listagg (rate) WITHIN GROUP (ORDER BY rate) as rate
FROM OptionEmbeddedBondRate
where s_info_windcode ='110031.SH'
GROUP BY s_info_windcode
但是这里会有一个问题,那就是rate里面的0.2 , 0.5前面的0不见了,这是number转varchar会出现的一个问题,我们可以这样:
通过substr获取该字段的 第一位,再通过decode判断是否为 “ . ” ,是的话,‘0’||字段 拼接; 否 返回 该字段 。
decode(substr((to_char(rate)),0,1),'.','0'||to_char(rate),to_char(rate))
结合起来就是下面这样:
SELECT s_info_windcode,
listagg (decode(substr((to_char(rate)),0,1),'.','0'||to_char(rate),to_char(rate)) , ',') WITHIN GROUP (ORDER BY rate) as rate
FROM OptionEmbeddedBondRate
where s_info_windcode ='110031.SH'
GROUP BY s_info_windcode
查询结果如下所示: