不使用DISTICT是因为真正的表不止A,B两个字段。还有一个C字段A    B     C
a1   b1    c1
a1   b1    c2
a1   b2    c3
a1   b3    c4 
a2   b1    c5
a2   b1    c6
a3   b2    c7
a3   b2    c8
a3   b3    c9如果是上面这种表结构,使用DISTINCT也可以.

解决方案 »

  1.   

    select a,sum(count) from 
    (select a,count(*) count from table group by a,substr(2,length(b)-1)) group by a;
      

  2.   

    select a,sum(count) from 
    (select a,count(*) count from table group by a,substr(2,length(b)-1)) group by a;好像不行,我运行结果如下A                    SUM(COUNT)
    -------------------- ----------
    a1                            4
    a2                            2
    a3                            3
      

  3.   

    这样不行吗,跟多少个字段有什么关系吗?
    select A,count(distinct B) from T1 group by A
      

  4.   

    就算3个或更多字段,结果也一样的
    A    B     C
    a1   b1    c1
    a1   b1    c2
    a1   b2    c3
    a1   b3    c4 
    a2   b1    c5
    a2   b1    c6
    a3   b2    c7
    a3   b2    c8
    a3   b3    c9select A,count(distinct B) from T1 group by A
    A  COUNT(DISTINCTB)
    a1  3
    a2  1
    a3  2
      

  5.   

    select A,count(distinct B) from T1 group by A这个可以,谢谢
      

  6.   

    select a,count(1) 
    from(
    select distinct a,b from t1
    )
    group by a;
      

  7.   

    用分析函数处理。SQL> select * from test;A     B
    ----- -----
    a1       b1
    a1       b1
    a1       b2
    a1       b3
    a2       b1
    a2       b1
    a3       b2
    a3       b2
    a3       b39 行 已选择SQL> 
    SQL> select a, count(*)
      2    from (select a, b
      3            from (select a,
      4                         b,
      5                         row_number() over(partition by a, b order by a) t
      6                    from test)
      7           where t = 1)
      8   group by a;A       COUNT(*)
    ----- ----------
    a1             3
    a2             1
    a3             2
      

  8.   

    哦,sorry 是
    select a,sum(count) from 
    (select a,1 count from table group by a,substr(2,length(b)-1)) group by a;