3

我遇到了一个 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 不是我的强项。

4

1 回答 1

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
于 2012-05-13T20:05:29.330 回答