字段1       字段2 
A            00 
A            01 
B            00 
B            01 
C            00 
C            02 
D            00 
D            02 
E            01 
E            02 
上面是表的模拟数据,分别统计出字段2为(00,01)、(00,02)、(01,02)三种情况的数据,比如上表对应三种情况的正确结果应该是2、2、1,这个sql语句应该如何写,各位大虾帮帮忙

解决方案 »

  1.   

    select 字段1,count(字段1) from table 
    group by 字段1
      

  2.   

    votoon兄弟,你可能没有明白我的意思
      

  3.   

    select sum(decode(aa.b,'00,01',1,0)) "(00,01)",
    sum(decode(aa.b,'00,02',1,0)) "(00,02)",
    sum(decode(aa.b,'01,02',1,0)) "(01,02)"
    from
    (
    select a,WMSYS.WM_CONCAT(b) b
    from table
    group by a) aa
      

  4.   

    可能是我表达问题,我的意思是根据字段1分组,比如前两个A第一组,两个B是第二组,两个C是第三组,两个D是第四组,两个E是第五组,每一组都对应字段2的两个值,只可能是(00,01)或者(00,02)或者(01,02)这三种情况中的一种,我需要统计出这三种情况分别有多少组,比如第一组和第二组就是(00,01),统计数据就是2,第三组和第四组是(00,02),统计数据也是2,第五组是(01,02),统计数据是1,这样说能明白吗,可能说得有点罗唆
      

  5.   

    我写的SQL可以统计出来的
    出来是一行
    结果
    (00,01) (00,02) (01,02)
     2         2         1
    你如果要分别统计,可以这样
    select '(00,01)' name,count(1) num
    from 

    select a,WMSYS.WM_CONCAT(b) b 
    from table 
    group by a) aa 
    where aa.b='00,01'
    union all
    select '(00,02)',count(1)
    from 

    select a,WMSYS.WM_CONCAT(b) b 
    from table 
    group by a) aa 
    where aa.b='00,02'
    union all
    select '(01,02)',count(1)
    from 

    select a,WMSYS.WM_CONCAT(b) b 
    from table 
    group by a) aa 
    where aa.b='01,02'这样出来的结果就是
    name      num
    (00,01)   2
    (00,02)   2
    (01,02)   1
      

  6.   

    SELECT  COUNT(tb.a2) num,
             decode(tb.a1,1,'(00,01)',
                          2,'(00,02)',
                          3,'(01,02)')
    FROM (
    SELECT sum(a2) a1,sum(a2) a2
    from x_temp 
    group by a1) tb
    GROUP BY a1
      

  7.   

    参考一下吧:
    SELECT COUNT(1),
           NT.A2 || XT.A2 AA2
    FROM   (select MAX(t1.A2) A2,
                   T1.A1
            from   x_temp t1
            GROUP  BY T1.A1) XT,
           (select MIN(t2.A2) A2,
                   T2.A1
            from   x_temp t2
            GROUP  BY T2.A1) NT
    WHERE  XT.A1 = NT.A1
    GROUP  BY NT.A2 || XT.A2
    我感觉比较灵活,但是只适合字段1相同的只能有2个
      

  8.   

    先写下思路,一会再解决
    order by 字段1,字段2再根据结果集合,连接成  
    col1 col2
    a  00,01
    b  00,01
    ...
    形式,最后根据上个结果集
    select col2,count(*) from ...
    group by col2
      

  9.   

    test(id,name)Select Type,Count(*) From (
    select id, substr(max(sys_connect_by_path(nm,',')),2) As Type from 
    (SELECT id, Name As nm, MIN(name) over(PARTITION BY Id) minnm
    ,(row_number() over(ORDER BY id, name)) + (dense_rank() over(ORDER BY id)) no
    FROM test)
    start with nm=minnm
    connect by no-1 = prior no 
    group by Id )
    Group By Type
      

  10.   

    create table att(c1 varchar(10),c2 varchar(10));
    insert into att values('A','00') ;
    insert into att values('A','01') ;
    insert into att values('B','00') ;
    insert into att values('B','01') ;
    insert into att values('C','00') ;
    insert into att values('C','02') ;
    insert into att values('D','00') ;
    insert into att values('D','02') ;
    insert into att values('E','01') ;
    insert into att values('E','02') ;
    SELECT a.c2||','||b.c2 m,count(*) c FROM ATT a,att b where a.c1 = b.c1 and a.c2<b.c2 
    group by a.c2||','||b.c2;
    drop table att;
    /*
    M                              C
    --------------------- ----------
    00,01                          2
    00,02                          2
    01,02                          1
    */
      

  11.   

    with a as(select 字段1,字段2 from 表名 ),b as (select 字段1,字段2 from 表名),c as (select distinct a.字段1,a.字段2 as "22" ,b.字段2 as "33" ,a.字段2|b.字段2  as "44",1 as "55" from a inner join b on(a.字段1=b.字段1)),select count("55"),"44" group by "44";
      

  12.   

    with a as(select 字段1,字段2 from 表名 ),
    b as (select 字段1,字段2 from 表名),
    c as  (select distinct a.字段1,a.字段2 as "22" ,b.字段2 as "33" ,a.字段2 |','|b.字段2  as "44",1 as "55" from a inner join b on(a.字段1=b.字段 1)),
    select count("55") as “C”,"44"  as "M"from c group by "44";
    得出的结果会有 M     C
                 00,01   2
                 00,02   2
                 01,02   1
                 01,00   2
                 02,00   2
                 00,01   1