1、源数据表结构
CREATE TABLE "sf_ref_pd_config" (
"I_BATCH_NO" decimal(5,0) DEFAULT NULL COMMENT '批次ID',
"V_ASSET_CLASS_NAME" varchar(200) DEFAULT NULL COMMENT '资产类型',
"N_EXEC_ID" decimal(5,0) DEFAULT NULL COMMENT '执行排序',
"N_PD_ID" decimal(10,0) DEFAULT NULL COMMENT 'PD_ID',
"V_FIELD1_BUSINESS_TYPE" varchar(2000) DEFAULT NULL COMMENT '业务品种'
) ENGINE=EXPRESS REPLICATED DEFAULT CHARSET=utf8 TABLESPACE='sys_tablespace' COMMENT='PD映射人工配置';
2、插入测试数据
INSERT INTO ifrs10.sf_ref_pd_config (I_BATCH_NO,V_ASSET_CLASS_NAME,N_EXEC_ID,N_PD_ID,V_FIELD1_BUSINESS_TYPE) VALUES
(4,'信用卡',NULL,2,'1110160')
,(5,'零售信贷',NULL,2,'1110090,1110160,1110161,1110380')
,(5,'信用卡',NULL,3,'1110084,1110086,1110089,1110360')
,(4,'零售信贷',NULL,3,'1110030,1110084,1110086')
,(1,'信用卡',NULL,3,'1110071,1110082,1110087,1110088,1110100,1110110,1110120,1110140,1110370,1110410,1110755,1110760,1110765,1110940,1110950,1110050,1110055,1110090,1110161,1110380')
,(6,'零售信贷',NULL,1,'1110050')
,(6,'信用卡',NULL,3,'1110360')
,(1,'零售信贷',NULL,1,'1110050')
;
3、新建关联表并插入大于逗号分隔符中的数据
create table xx_sequence(id int);
-- 插入数据(列数要大于逗号分隔符中的数据)
insert into xx_sequence
select rowid from ex_ods_bond_credit;
4、行列转换查询
SELECT N_PD_ID,
I_BATCH_NO,
V_ASSET_CLASS_NAME,
SUBSTRING_INDEX(SUBSTRING_INDEX(A.V_FIELD1_BUSINESS_TYPE,',',id+1),',',-1) col_new
FROM sf_ref_pd_config a
inner join xx_sequence tl
on t1.id < LENGTH(A.V_FIELD1_BUSINESS_TYPE)-LENGTH(REPLACE(A.V_FIELD1_BUSINESS_TYPE,',',''))+1;
5、后记
逗号分隔字符数据的又一种行列转换方式,脚本是源于Gbase数据库的,应该同样适用于Oracle。
附(原始SQL):
SELECT
I_BATCH_NO,V_ASSET_CLASS_NAME,N_PD_ID,SUBSTRING_INDEX(SUBSTRING_INDEX(A.V_FIELD1_BUSINESS_TYPE,',',id+1),',',-1) AS num
FROM
(select I_BATCH_NO,V_ASSET_CLASS_NAME,N_PD_ID,V_FIELD1_BUSINESS_TYPE from sf_ref_pd_config ) A join
t1 WHERE
t1.id < LENGTH(A.V_FIELD1_BUSINESS_TYPE)-LENGTH(REPLACE(A.V_FIELD1_BUSINESS_TYPE,',',''))+1 ;
及内网SQL图片:
关联文章:一分多行列转换(逗号分隔的一列数据转换成多行)-CSDN博客
一分多行列转换-CSDN博客