一个字段有多个值,值通过分隔符分割

2020-11-12  本文已影响0人  盗生一

-- 可以拆分为多条数据

SELECT distinct REGEXP_SUBSTR(t.cym, '[^、]+', 1, level) cym, t.* from person_info t connect by level <= regexp_count(t.cym, '、') + 1;

-- 人员统计
select cym, count(1) from (SELECT distinct REGEXP_SUBSTR(t.cym, '[^、]+', 1, level) cym, t.ryxxbz, t.xm from person_info t connect by level <= regexp_count(t.cym, '、') + 1) group by cym;

-- 人员统计
select 

SUM( CASE WHEN cym = '重点' THEN  1 ELSE 0 END),  --男性人口  
SUM( CASE WHEN cym <> '重点' THEN  1 ELSE 0 END)   --女性人口  

from (SELECT distinct REGEXP_SUBSTR(t.cym, '[^、]+', 1, level) cym, t.ryxxbz, t.xm from person_info t connect by level <= regexp_count(t.cym, '、') + 1) group by cym;

select 

 SUM( CASE WHEN cym = '重点' THEN  1 ELSE 0 END),  --男性人口  
SUM( CASE WHEN cym <> '重点' THEN  1 ELSE 0 END)   --女性人口  

from (SELECT distinct REGEXP_SUBSTR(t.cym, '[^、]+', 1, level) cym, t.ryxxbz, t.xm from person_info t connect by level <= regexp_count(t.cym, '、') + 1) group by cym;
上一篇 下一篇

猜你喜欢

热点阅读