简体   繁体   English

如何在MS Access的单个查询中在一个字段中找到最小值,在另一个字段中找到关联值?

[英]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.

相关问题 如何根据另一个字段的值过滤 MS Access 字段 - How to filter MS Access field based on the value of another field 如何使用 MS Access 中的 VBA 使用另一个表中的数据更新单个记录的单个字段 - How do I update a single field of a single record with data from another table with VBA in MS Access MS Access中查询的字段默认值 - Field default value from query in MS Access ms Access 2007在查询中组合了多值字段和单值字段 - ms access 2007 combining a multi-valued field with a single-value field in query 如何基于“表1”上的“字段值”和“字段名称”在MS Access“表2”中查找值 - How to find a Value in MS Access “Table 2” based on “a field value” & “a field name” on “Table 1” 如何在 MS Access 中使用 SQL 查询将字段值增加 1 - How can I increment a field value by 1 using SQL Query in MS Access 如何在 MS Access 中使用带有 NULL 值的日期字段的 LIKE - How do I use LIKE with a date field in MS Access with a NULL value 如何查询行中是否存在值,将值添加到另一个字段? - How do I make a query for if value exists in row add a value to another field? 如何使用文本框的值填充 MS Access 查询 - How do I populate a MS Access Query with a value of a Text Box MS Access-Dlookup根据另一个表的内容设置字段的值 - MS Access - Dlookup to set value of field based on content of another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM