举例:select code,id,from t group by code的结果如下
A 1
A 1
A 2我现在想做的是统计不重复的id的个数,请问sql如何写。
请高手指点!谢谢!

解决方案 »

  1.   

    select code,id,from t group by code
    --------你这条语句能执行成功?把你的表结构、测试数据和最终想要的数据贴出来吧
      

  2.   

    select distinct id from t
      

  3.   

    select count(distinct id) from t
      

  4.   

    select code,count(distinct id) from t group by code
      

  5.   

    写错了是:select code,id,from t order by code我已经想到了方法:select code,count(*) from (select distinct id,code from t order by code) a 不知道有没有更效率的写法
      

  6.   

    按code分组?
    那就select code,count(distinct id) from t group by code order by code
      

  7.   

    select code,count(*) from (select distinct id,code from t order by code) a 
    ----你这句也不行。select code,count(distinct id) from t group by code order by code
    不知道这是不是你想要的结果。。
      

  8.   

    谢谢大家,
    qin_phoenix、wildwave、liusong_china都能实现。