向导生成的查询中的Access 2007错误,用于查找重复项

时间:2014-08-04 16:02:39

标签: sql ms-access ms-access-2007

我正在尝试使用查询向导生成查询以查找重复记录。使用表生成的查询工作正常,但是当我再次应用向导在查询中运行查找重复时,我不断收到此错误: :“您尝试执行不包含指定表达式的查询...作为聚合函数的一部分”。 这是向导生成的SQL:

SELECT [four letter c-codes].[4_letter_code], [four letter c-codes].[bird_data_ID], [four letter c-codes].[Metal_ID]
FROM [four letter c-codes]
WHERE ((([four letter c-codes].[4_letter_code]) In (SELECT [4_letter_code] FROM [four letter c-codes] As Tmp GROUP BY [4_letter_code] HAVING Count(*)>1 )))
ORDER BY [four letter c-codes].[4_letter_code];

我尝试单独执行In子句并且它工作正常,输出字段[4_letter_code]的重复值。用由此产生的常数替换In子句也给出了期望的输出。 非常感谢任何帮助解决这个问题...

1 个答案:

答案 0 :(得分:1)

我刚刚删除了多余的括号,试试这个......

SELECT  [four letter c-codes].[4_letter_code] ,
        [four letter c-codes].[bird_data_ID] ,
        [four letter c-codes].[Metal_ID]
FROM    [four letter c-codes]
WHERE   [four letter c-codes].[4_letter_code] IN (
        SELECT  [4_letter_code]
        FROM    [four letter c-codes]
        GROUP BY [4_letter_code]
        HAVING  COUNT(*) > 1 )
ORDER BY [four letter c-codes].[4_letter_code];

或者,您可以尝试INNER JOIN ...

SELECT  [four letter c-codes].[4_letter_code] ,
        [four letter c-codes].[bird_data_ID] ,
        [four letter c-codes].[Metal_ID]
FROM    [four letter c-codes]
        INNER JOIN ( SELECT [4_letter_code]
                     FROM   [four letter c-codes]
                     GROUP BY [4_letter_code]
                     HAVING COUNT(*) > 1
                   ) b 
             ON [four letter c-codes].[4_letter_code] = b.[4_letter_code]
ORDER BY [four letter c-codes].[4_letter_code];