我遇到了SQL查询的问题。我有145个站点名称的列表。在每个地点都发现了某些物种。我已经计算了每个地点每个物种被看到的次数。我想找到每个地点看到的5种最常见的物种。目前我有:
SELECT TOP 5 Count([bird point counts bound query].[Group size]) AS [CountOfGroup size], [bird point counts bound query].site, [bird point counts bound query].Species
FROM [bird point counts bound query]
GROUP BY [bird point counts bound query].site, [bird point counts bound query].Species
ORDER BY Count([bird point counts bound query].[Group size]) DESC;
这只是从所有地点返回5种最常见的物种。只是为了澄清,每个站点和145个站点的前5个结果,结果表应该包含725条记录。我现在在Access工作。
任何帮助,因为SQL不是我的强项。
答案 0 :(得分:0)
据我所知,你只能在Access中使用临时表来完成这项工作。
这些方面的东西?
CREATE TABLE ##SiteSpecies (
id int IDENTITY (1,1),
Site <whatever>,
Species <whatever>,
Observations int
)
INSERT INTO ##SiteSpecies
SELECT [bird point counts bound query].site, [bird point counts bound query].Species, Count([bird point counts bound query].[Group size])
FROM [bird point counts bound query]
GROUP BY [bird point counts bound query].site, [bird point counts bound query].Species
ORDER BY [bird point counts bound query].site, [bird point counts bound query].Species, Count([bird point counts bound query].[Group size])
SELECT
results.*
FROM
##SiteSpecies AS results
INNER JOIN
(
SELECT Site, MIN(id) AS FirstID FROM ##SiteSpecies GROUP BY Site
)
AS SiteMarkers
ON results.Site = SiteMarkers.Site
AND results.id <= SiteMarkers.FirstID + 4