日期:2014-05-17  浏览次数:20742 次

选择列表中的列 'Characteristic.CharacteristicCode' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
select Name,CharacteristicCode from Characteristic where ProductGroupCode='PLBDT' and (Status = -1 or Status=1) group by Name 我想用这条sql语句查出不重复的数据(字段Name中的数据有重复),该怎么写呢?

------解决方案--------------------
直接加 DISTINCT 不可以吗

SQL code
select DISTINCT  Name,CharacteristicCode from Characteristic where ProductGroupCode='PLBDT' and (Status = -1 or Status=1)

------解决方案--------------------
name有重复那么CharacteristicCode有没有重复呢?
如果只是name有重复,CharacteristicCode没有,那你想去掉重复的name,CharacteristicCode就要有个选取标准,是取最大值还是最小值,加个max就可以了。
如果都重复的话,那么直接distinct就可以了。
------解决方案--------------------
SQL code

select Name,CharacteristicCode 
from Characteristic  AS A
where ProductGroupCode='PLBDT' and (Status = -1 or Status=1) and CharacteristicCode = (SELECT TOP 1 CharacteristicCode FROM Characteristic WHERE A.Name = Name)