在sqlserver2005中无法验证这句话:包含 GROUP BY 的子查询不能使用 DISTINCT 关键字。
请各位大侠帮忙验证!
以下代码在sqlserver2005中可以执行:
select distinct teamname from score
where teamName in
(select distinct teamName from score group by teamname)
group by teamname

解决方案 »

  1.   

    distinct 和 group by 合用有啥意义?
      

  2.   

    你就一个字段group by和distinct的效果一样,没有体现你分组的意义。
    distinct是去重,group by 是分组。
      

  3.   

    子查询用group by ,父查询中用distinct针对其他列。
      

  4.   

    针对其他列
    select
      *
    from
      tb t
    where
      id=(select max(id) from tb where teamName=t.teamName)
      

  5.   

    http://msdn.microsoft.com/zh-cn/library/ms189543(SQL.90).aspx第5条。
      

  6.   

    难以理解为什么有这句话,就算不是字查询,你也无法distinct 一个非group by 的字段啊
      

  7.   

    给你问了一下:
    http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/17988c0f-0df4-4063-a0b3-82b21d22fb2b正如你说的,这个规则应该是错误的。
      

  8.   

    group by + sum,avg,max等的
    使用的环境不同,一般在统计的时候就用到GROUP BY
    而另一个就是去重复的.
    如果说在筛选的情况下,两者可以通用!
      

  9.   

    select teamname from score 
    where teamName in 
    (select distinct teamName from score group by teamname) 
    group by teamname ======================
    select distinct  teamname from score 
    where teamName in 
    (select distinct teamName from score group by teamname)