select ambiguity_name
from TBL_SAL_CUSTOMER 
group by ambiguity_name 
having count(distinct template_id)=1select ambiguity_name
from TBL_SAL_CUSTOMER 
group by ambiguity_name
having count(template_id)=1
谁能说一下having count(distinct template_id)=1 与having count(template_id)=1 有啥区别

解决方案 »

  1.   

    having count(distinct template_id)=1
    去除了重复 template_id
      

  2.   

    结果都一样的,是对group 之后的结果集过滤。
      

  3.   

    create table #(id int, a varchar)
    insert # select 1, 'a'
    insert # select 2, 'a'
    insert # select 3, 'b'select a, count(distinct a) from # group by a
    /*
    a    
    ---- -----------
    a    1
    b    1
    */
    select a, count(a) from # group by a
    /*
    a    
    ---- -----------
    a    2
    b    1
    */
      

  4.   

    作为 having赛选条件时候
    count(distinct template_id)=1 → 去除了重复 template_id 指的是只有唯一的template_id才显示是吧
    count(template_id)=1 显示的不也是只有唯一的template_id吗?因为前边的group by 已经把ambiguity_name分完类了