sql - Oracle How to get the count of occurrences within a comma separated string -
sql - Oracle How to get the count of occurrences within a comma separated string -
i have string comma separated value stored in table. want description , count of each item in string. so, if 1 record has string 'yl', want 'yl description', 1. if record has 'yl,yb,yb', want 'yl description', 1 , 'yb description', 2.
using next false tables create data:
create table temp1 (cd_vals varchar2(20 byte)); insert temp1 (cd_vals) values ('yb,yl'); insert temp1 (cd_vals) values ('yb,yl,yl,yl'); insert temp1 (cd_vals) values ('yl'); create table temp2 (cd_val varchar2(2 byte), cd_desc varchar2(20 byte)); insert temp2 (cd_val, cd_desc) values ('yb','yb description'); insert temp2 (cd_val, cd_desc) values ('yl','yl description');
i've got query returns each value table1, lookup description, , count of instances in each original string. (split might function internal our system, takes comma delimited string , returns each entry. should able utilize in solution, if can wrap head around it.)
using query:
select t1.cd_vals , t2.cd_desc , regexp_count(t1.cd_vals, '[^,]+') temp1 t1 bring together temp2 t2 on rtrim(t2.cd_val) in (select column_value table(split(rtrim(t1.cd_vals)))) order cd_vals;
i next results:
cd_vals cd_desc count yb,yl yb description 2 yb,yl yl description 2 yb,yl,yl,yl yl description 4 yb,yl,yl,yl yb description 4 yl yl description 1
but want is:
cd_vals cd_desc count yb,yl yb description 1 yb,yl yl description 1 yb,yl,yl,yl yl description 1 yb,yl,yl,yl yb description 3 yl yl description 1
how lastly count field shows count of specific lookup value in first string?
related, doesn't quite seem i'm looking count number of occurrences of keyword in comma separated column? , how count of occurrence comma separated string. (or maybe i'm not quite looking @ them right.)
is want?
select cd_vals, cd_val, count(1) (select cd_vals,column_value cd_val temp1 t1, table(split(rtrim(t1.cd_vals)))) bring together temp2 using (cd_val) grouping cd_vals, cd_val cd_vals cd_val count(1) ----------- ------ -------- yb,yl yb 1 yb,yl,yl,yl yl 3 yl yl 1 yb,yl yl 1 yb,yl,yl,yl yb 1 5 rows returned.
sql string oracle oracle11g
Comments
Post a Comment