简体   繁体   中英

Microsoft Access 2010: Select most recent max Record ID for each LANID

I need to filter out this data based on some criteria.

  1. For every unique LANID, a user can have up to 2 records. Some users will only have 1 record.
  2. I need to select the max Record ID for each LANID.

在此处输入图片说明

因此,创建一个查询以确定按LANID分组时的max(recordID) ,然后使用第二个查询作为第一个查询作为数据源,将其连接回到基于LANIDmax(recordID)

Assuming the last update date is not duplicated for a given row, then one method is to use a correlated subquery to get the last date and then get the rest of the columns in the row:

select sd.*
from sampleData as sd
where sd.RecordId = (select max(sd2.RecordId)
                     from sampleData as sd2
                     where sd2.lanId = sd.lanId
                    );

EDIT:

If you wanted the largest record id for the most recent update date:

select sd.*
from sampleData as sd
where sd.RecordId = (select top 1 sd2.RecordId
                     from sampleData as sd2
                     where sd2.lanId = sd.lanId
                     order by sd2.lastUpdateDate desc, sd2.RecordId desc
                    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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