ACCESS 2010 SQL--在SELECT TOP子查询字段上使用WHERE IN

时间:2013-11-27 16:00:32

标签: sql select subquery ms-access-2010 where-clause

我希望此查询能够告诉我2013年2013年前十大消费者公司的支出!

SELECT [Company], 
       Sum([SPENDING])
FROM   [Data]
WHERE  [Company] IN (
                     SELECT TOP 10 [Company]
                     FROM          [Data]
                     WHERE         [Year] IN ("2013")
                     GROUP BY      Company
                     ORDER BY      Sum([SPENDING]) DESC
                     )
       AND [Year] IN ("2012")
GROUP BY Company
;

当我尝试运行它时,我没有收到任何错误,但Access表示它正在“运行查询”并且永远不会完成。数据的大小不是问题。

这是我找到的最接近的例子,但它并没有真正解答答案:MS Access - WHERE IN works, but WHERE NOT IN fails

1 个答案:

答案 0 :(得分:4)

我怀疑这只是Access优化器的限制。请尝试这样:

SELECT d.[Company], 
       Sum(d.[SPENDING])
FROM   [Data] As d
INNER JOIN    (
                     SELECT TOP 10 [Company]
                     FROM          [Data]
                     WHERE         [Year] IN ("2013")
                     GROUP BY      Company
                     ORDER BY      Sum([SPENDING]) DESC
              ) As t  ON  t.Company = d.Company
WHERE d.[Year] IN ("2012")
GROUP BY d.Company