您好,欢迎访问一九零五行业门户网

oracle—SQL技巧之(二)WMSYS.WM_CONCAT函数实现多行记录用逗号拼

由于业务系统的交易记录有很多,常常有些主管需要看到所有的记录情况;又不想滚动;接下来介绍使用oracle自带的函数 wmsys.wm_concat,进行拼接,感兴趣的朋友可以了解下
需求:
目前接触bi系统,由于业务系统的交易记录有很多,常常有些主管需要看到所有的记录情况,但是又不想滚动,想一眼就可以看到所有的,于是就想到了字符串拼接的形式。
解决方案:使用oracle自带的函数 wmsys.wm_concat,进行拼接。
函数限制:它的输出不能超过4000个字节。
为了不让sql出错,又可以满足业务的需求,超过4000个字节的部分,使用“。。。”
实现sql如下:
代码如下:
create table tmp_product
(product_type varchar2(255),
product_name varchar2(255));
insert into tmp_product
select 'a','producta'||rownum from dual
connect by level union all
select 'b','productb'||rownum from dual
connect by level union all
select 'c','productc'||rownum from dual
connect by level union all
select 'd','productd'||rownum from dual
connect by level union all
select 'e','producte'||rownum from dual
connect by level 代码如下:
select product_type,
wm_concat(product_name) || max(str) as product_multi_name
from (select product_type,
product_name,
case
when all_sum > 4000 then
'...'
else
null
end as str
from (select product_type,
product_name,
sum(vsize(product_name || ',')) over(partition by product_type) as all_sum,
sum(vsize(product_name || ',')) over(partition by product_type order by product_name) as up_sum
from tmp_product)
where (up_sum 4000)
or all_sum group by product_type
其它类似信息

推荐信息