简体   繁体   English

MSACCESS SQL区别查询

[英]MSACCESS SQL Distinct Query

I am using MS Access 2010 and am stuck. 我正在使用MS Access 2010并被卡住。 have a table with two columns FileID and Name for which there can be multiple names for a FileID. 有一个包含两列FileID和Name的表,其中FileID可以有多个名称。 I need to obtain the distinct Name, and a FileID for each Name. 我需要获取不同的名称,并为每个名称获取一个FileID。 I do not care which FileID is returned as long as one is returned. 我不在乎只要返回一个FileID。

Table: 表:
FileID, Name FileID,名称
1, John 1,约翰
1, John 1,约翰
2, John 2,约翰
3, Simon 3,西蒙
4, David 4,大卫

I would expect the result of 我希望得到的结果
3, Simon 3,西蒙
4, David 4,大卫
and either 1, John or 2, John. 1个或2个。

Would really really appreciate any advice on how this can be done in a query. 真的很感谢在查询中如何完成此操作的任何建议。

You can do this with an aggregation function: 您可以使用聚合函数执行此操作:

select name, min(fileid) as fileid
from [table] t
group by name;

Access also supports first() and last() . Access还支持first()last() So, first() will get the first value encountered (as opposed to the minimum value): 因此, first()将获得遇到的第一个值(而不是最小值):

select name, first(fileid) as fileid
from [table] t
group by name;

Let me know if this works for you: 让我知道这是否适合您:

SELECT FileID, Name FROM [table] WHERE [conditions] GROUP BY Name;

This will return only 1 FileID, and return a record for every unique Name 这将仅返回1个FileID,并为每个唯一名称返回一条记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM