1

您好我正在尝试对物种数据库进行查询。我想查询唯一值,但也想找到第一次发现物种的年份。

到目前为止,我有这个:

SELECT DISTINCT [Genus_HeTR] & " " & [Species_HeTR] AS Species
FROM HeTR_Rec
WHERE [Species_HeTR] <> "sp."
UNION SELECT DISTINCT [Genus_HeOP] & " " & [Species_HeOP] AS Species
FROM HeOP_Rec
WHERE [Species_HeOP] <> ""
AND [Species_HeOP] <> "sp.";

我正在连接属名和物种名,并从两个不同的表中添加数据(因此是 UNION)。这提供了一个物种列表,但我想知道该物种在该站点出现的年份。

4

1 回答 1

1

我会冒险猜测您的两个源表都包含一个日期/时间字段,该字段存储每个观察的日期。如果是这样,您可以UNION从 2 个表中获取数据,并将其用作查询中的子查询源GROUP BY,您可以在其中得出每个物种的最小观察年份。

SELECT
    sub.Species,
    Min(sub.observation_year) AS first_sighting_year
FROM
    (
        SELECT
            [Genus_HeTR] & " " & [Species_HeTR] AS Species,
            Year(observation_date) AS observation_year
        FROM HeTR_Rec
        WHERE [Species_HeTR] <> "sp."
        UNION ALL
        SELECT
            [Genus_HeOP] & " " & [Species_HeOP] AS Species,
            Year(observation_date) AS observation_year
        FROM HeOP_Rec
        WHERE [Species_HeOP] <> ""
        AND [Species_HeOP] <> "sp."
    ) AS sub
GROUP BY sub.Species;
于 2013-04-17T21:23:02.043 回答