问题
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