将逗号分割的clob字段转化为varchar,并将字符串拆分为多行
2015-05-11 16:52
369 查看
SELECT '远大洪雨' 企业名称, sgpro.project_name 工程名称, pur.product_type 产品类别, pur.product_name 产品名称, pur.purchase_num 涉及数量, pur.product_regdate 材料进场时间, sgpro.build_licence 施工许可证号, sgpro.contractor_name 总包单位名称 FROM t_purchase_info pur, t_allproject_info sgpro WHERE pur.project_id = sgpro.id AND pur.id in (select distinct regexp_substr(ids, '[^,]+', 1, level) project_id from (select dbms_lob.substr(project_ids) ids from g_enterprise_score where class in (3, 4) and score_type = 5 AND bad_no = 1 AND enterprise_id in (select distinct id from bemms.g_enterprise_info where name like '%远大洪雨%') and project_ids is not null) connect by level <= length(dbms_lob.substr(ids)) - length(replace(dbms_lob.substr(ids), ',', '')) + 1 )
其中,将clob转化为varchar,利用dbms_lob.substr,例:
select dbms_lob.substr(project_ids) from g_enterprise_score where class in (3, 4) and score_type = 5 AND bad_no = 1 AND enterprise_id in (select distinct id from bemms.g_enterprise_info where name like '%远大洪雨%') and project_ids is not null ;
将一行字符串(逗号分割)转化多行,利用regexp_substr() connect by level,例:
select distinct regexp_substr(ids, '[^,]+', 1, level) project_id from (select dbms_lob.substr(project_ids) ids from g_enterprise_score where class in (3, 4) and score_type = 5 AND bad_no = 1 AND enterprise_id in (select distinct id from bemms.g_enterprise_info where name like '%远大洪雨%') and project_ids is not null) connect by level <= length(dbms_lob.substr(ids)) - length(replace(dbms_lob.substr(ids), ',', '')) + 1
以上SQL执行效率特别低,我们欢欢大神说过: 越是高级的函数越有可能影响效率,还是最基础的函数好,效率杠杠的。
其中尽量少用in,用exists,或者instr,经过优化后的SQL如下:
SELECT '北京XXX股份有限公司' 企业名称, sgpro.project_name 工程名称, pur.product_type 产品类别, pur.product_name 产品名称, pur.purchase_num 涉及数量, pur.product_regdate 材料进场时间, sgpro.build_licence 施工许可证号, sgpro.contractor_name 总包单位名称 FROM t_purchase_info pur, t_allproject_info sgpro WHERE pur.project_id = sgpro.id AND instr((select ',' || wm_concat(dbms_lob.substr(g.project_ids)) || ',' ids from g_enterprise_score g where exists (select id from bemms.g_enterprise_info e where g.enterprise_id = e.id and name like '%北京%东方雨虹%') and (g.class = 3 or g.class = 4) and g.score_type = 5 AND g.bad_no = 1 and project_ids is not null), ',' || pur.id || ',') > 0
相关文章推荐
- 分割字符串的几个方法如何将多个逗号字符串拆分成多条数据
- sqlserver以逗号分割的字符串拆分到临时表
- C#以逗号拆分字符串,若字段中包含逗号(备注:包含逗号的字段必须有双引号引用)则对其进行拼接处理
- C#以逗号拆分字符串,若字段中包含逗号(备注:包含逗号的字段必须有双引号引用)则对其进行拼接处理
- 存储过程拆分Patents表Inventor字段逗号分隔字符串
- oracle sql查询字段中以“;”分割的字符串,转化成对应的,以“;”分割的文字串
- Oracle利用SQL将clob字段数据转化为字符串
- C#以逗号拆分字符串,若字段中包含逗号(备注:包含逗号的字段必须有双引号引用)则对其进行拼接处理
- 逗号分割的字段值拆分
- java中如何将字符数组转换成字符串,以逗号分割
- 字符串分割+日期和整数的转化
- 逗号和分号分割字符串
- 将逗号分隔 的字符串转化成List
- Oracle Clob字段保存时提示字符串过长
- mysql 字段值逗号分割后sql匹配
- 如何把一段逗号分割的字符串转换成一个数组
- 需求:MYSQL表中一个字段用逗号分隔,需要查出来split拆分并依次存入另一个表的一个字段中
- java 中用split分割字符串,最后的空格等不被拆分。
- 第一章 1.如何把一段逗号分割的字符串转换成一个数组?
- 0基础lua学习(二十一)分割字符串逗号