[英]How do I find a minimum value in a field and an associated value in an another field in a single query in MS Access?
I have a database of European countries. 我有欧洲国家的数据库。 In the field on the very right, I have the population densities of all the listed countries. 在最右边的字段中,我具有所有列出的国家的人口密度。 I have to find the smallest population density and display it and the country it belongs to in one query. 我必须找到最小的人口密度,并在一个查询中显示它及其所属的国家。
My best attempt: 我最好的尝试:
SELECT Min(europa.Nepsuruseg) AS MinOfNepsuruseg, europa.Orszag
FROM europa
GROUP BY europa.Orszag;
This, however, displays all the countries and their population densities. 但是,这显示了所有国家及其人口密度。 How do I do this right? 我该怎么做对?
You will first need to select the minimum population density across all records using a subquery. 您首先需要使用子查询在所有记录中选择最小人口密度。
Then in order to obtain all associated information held by the record or records whose population density field is equal to the minimum, you can either join the subquery to the main table, or include the subquery in the WHERE
clause. 然后,为了获取该记录或总体密度字段等于最小值的记录所拥有的所有关联信息,您可以将子查询连接到主表,或者将该子查询包括在WHERE
子句中。
Here is an example using an INNER JOIN
on the subquery: 这是在子查询上使用INNER JOIN
的示例:
select t.*
from europa t inner join
(
select min(e.nepsuruseg) as mpd from europa e
) q
on t.nepsuruseg = q.mpd
Or, using the WHERE
clause: 或者,使用WHERE
子句:
select t.*
from europa t
where t.nepsuruseg = (select min(e.nepsuruseg) from europa e)
You need to use the TOP function to grab just the smallest one, after you sort by Nepsuruseg. 在按Nepsuruseg排序后,您需要使用TOP函数仅捕获最小的函数。
SELECT Top 1 europa.Nepsuruseg, europa.Orszag
FROM europa
Order By europa.Nepsuruseg ASC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.