ACCESS 2010 SQL— using WHERE IN on SELECT TOP subquery field

妖精的绣舞 提交于 2019-12-14 01:26:00

问题


I want this query to tell me the spending in 2012 of the companies who were the top 10 spenders in 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
;

When I try to run it, I get no errors, but Access says it is "running query" and never finishes. The size of the data is not the problem.

This is the closest example I found, but it doesn't really give light to the answer: MS Access - WHERE IN works, but WHERE NOT IN fails


回答1:


I suspect that this is just a limitation of Access's optimizer. Try it like this instead:

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


来源:https://stackoverflow.com/questions/20247235/access-2010-sql-using-where-in-on-select-top-subquery-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!