您好我正在尝试对物种数据库运行查询。我想查询唯一值,但也找到物种最初找到的年份。
到目前为止,我有这个:
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.";
我正在连接Genus和物种名称并从两个不同的表中添加数据(因此是UNION)。这提供了物种清单,但我想知道在这个地方看到物种的年份。
答案 0 :(得分:1)
我猜测你的源表都包含一个日期/时间字段,用于存储每次观察的日期。如果是这样,您可以{2}表格中的UNION
数据,并将其用作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;