a(字段) 
01,02
01,03
01,04,02
02
01,02,03,04
02,04
就这么一个字段 求如何通过sql统计出
01出现的次数
02出现的次数
03 出现的次数
04出现的次数一个sql语句实现

解决方案 »

  1.   

    那 很简单   只要INSTR就可以了INSTR若是存在01就返回非0  否则返回0
      

  2.   

    SQL> select
      2  sum(decode(instr(name,'01'),0,0,1)),
      3  sum(decode(instr(name,'02'),0,0,1)),
      4  sum(decode(instr(name,'03'),0,0,1)),
      5  sum(decode(instr(name,'04'),0,0,1)) from t
      6  ;SUM(DECODE(INSTR(NAME,'01'),0, SUM(DECODE(INSTR(NAME,'02'),0, SUM(DECODE(INSTR(NAME,'03'),0, SUM(DECODE(INSTR(NAME,'04'),0,
    ------------------------------ ------------------------------ ------------------------------ ------------------------------
                                 4                              5                              2                              3SQL>
      

  3.   

    select sum(length(a) - nvl(length(replace(a,'01')),0)/2 as "01",
           sum(length(a) - nvl(length(replace(a,'02')),0)/2 as "02",
           sum(length(a) - nvl(length(replace(a,'03')),0)/2 as "03",
           sum(length(a) - nvl(length(replace(a,'04')),0)/2 as "04"
     from t;
      

  4.   


     
    select sum(length(a) - nvl(length(replace(a,'01')),0)/2 as "01",
           sum(length(a) - nvl(length(replace(a,'02')),0)/2 as "02",
           sum(length(a) - nvl(length(replace(a,'03')),0)/2 as "03",
           sum(length(a) - nvl(length(replace(a,'04')),0)/2 as "04"
     from t;方法有缺陷:
    如果存在01,02,0101,0102这样的记录,会错,
    正确的方法应该是按照数据的规则,用“,”分割才行。,估计至少要写个Function。。否则一味追求一条SQL语句,会很复杂难懂。
      

  5.   

    -- Create table
    create table T
    (
      A VARCHAR2(50)
    )
    //执行语句
    select "a",(
    select  sum((select sum(decode(substr(a,rownum,2),w."a",1,0))
    from dual connect by  level <=length(a)
    )) as "c" from  t) as "q"
    from (select distinct substr(a,1,2) as "a" from t) w
      

  6.   

    select tc2.code,count(tc1.*) from tbl tc1,(select distinct a code from tbl) tc2
    where tc1.a=tc2.code
    group by tc2.code上面tbl为表名,a为字段名
      

  7.   

    select
      sum((lengthb(a) - repleace(a,',01,',''))/4) "01",
      sum((lengthb(a) - repleace(a,',02,',''))/4) "02",
      sum((lengthb(a) - repleace(a,',03,',''))/4) "03",
      sum((lengthb(a) - repleace(a,',04,',''))/4) "04"
    from 
    (select ','||replace(l.a,',',',,'||',' a
    from table_name l)
      

  8.   

    select '01->'||to_char(count(1)) from t_temp where instr(a,'01')>0 
    union
    select '02->'||to_char(count(1)) from t_temp where instr(a,'02')>0 
    union
    select '03->'||to_char(count(1)) from t_temp where instr(a,'03')>0 
    union
    select '04->'||to_char(count(1)) from t_temp where instr(a,'04')>0 
    union
    select '05->'||to_char(count(1)) from t_temp where instr(a,'05')>0 
    union
    select '06->'||to_char(count(1)) from t_temp where instr(a,'06')>0 
    每行记录号,不能有得复记录
      

  9.   

    确实有点搞怪,应用SQL中的字符串函数和统计函数应该可以解决。
      

  10.   

    //****封装成函数create or replace function FUN_XT_YYBSJQX_POPEDOM(v_YourFileds in varchar2) return number is  v_Count number;
      v_strSQL varchar2(3000);
       being
       
       v_strSQL='select sum(length(a) - nvl(length(replace(a,''|| v_YourFileds  ||'')),0)/2 as mCount,
       from t';
        
       execute immediate v_strSQL into v_Count;   return v_Count;
       
       end;//****调用上面函数就可以了.