需求:
目前接触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 < 100
union all
select ‘b’,’productb’||rownum from dual
connect by level < 300
union all
select ‘c’,’productc’||rownum from dual
connect by level < 400
union all
select ‘d’,’productd’||rownum from dual
connect by level < 500
union all
select ‘e’,’producte’||rownum from dual
connect by level < 600;
复制代码 代码如下:
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 <= 3998 and all_sum > 4000)
or all_sum <= 4001)
group by product_type