简体   繁体   English

SQL 加入MS ACCESS最新记录

[英]SQL Join to the latest record in MS ACCESS

I want to join tables in MS Access in such a way that it fetches only the latest record from one of the tables.我想以这样一种方式加入 MS Access 中的表,即它只从其中一个表中获取最新记录。 I've looked at the other solutions available on the site, but discovered that they only work for other versions of SQL. Here is a simplified version of my data:我查看了网站上可用的其他解决方案,但发现它们仅适用于 SQL 的其他版本。这是我的数据的简化版本:

PatientInfo Table:患者信息表:

    +-----+------+
    | ID  | Name |
    +-----+------+
    |  1  | John |
    |  2  | Tom  |
    |  3  | Anna |
    +-----+------+

Appointments Table约会表

    +----+-----------+
    | ID |   Date    |
    +----+-----------+
    |  1 | 5/5/2001  |
    |  1 | 10/5/2012 |
    |  1 | 4/20/2018 |
    |  2 | 4/5/1999  |
    |  2 | 8/8/2010  |
    |  2 | 4/9/1982  |
    |  3 | 7/3/1997  |
    |  3 | 6/4/2015  |
    |  3 | 3/4/2017  |
    +----+-----------+

And here is a simplified version of the results that I need after the join:这是加入后我需要的结果的简化版本:

    +----+------+------------+
    | ID | Name |    Date    |
    +----+------+------------+
    |  1 | John |  4/20/2018 |
    |  2 | Tom  |  8/8/2010  |
    |  3 | Anna |  3/4/2017  |
    +----+------+------------+

Thanks in advance for reading and for your help.预先感谢您的阅读和帮助。

You can use aggregation and JOIN :您可以使用聚合和JOIN

select pi.id, pi.name, max(a.date)
from appointments as a inner join
     patientinfo as pi
     on a.id = pi.id
group by pi.id, pi.name;

something like this: select P.ID, P.name, max(A.Date) as Dt from PatientInfo P inner join Appointments A on P.ID=A.ID group by P.ID, P.name像这样:select P.ID, P.name, max(A.Date) as Dt from PatientInfo P inner join Appointments P.ID=A.ID group by P.ID, P.name

Both Bing and Gordon's answers work if your summary table only needs one field (the Max(Date)) but gets more tricky if you also want to report other fields from the joined table, since you would need to include them either as an aggregated field or group by them as well.如果您的汇总表只需要一个字段(Max(Date)),Bing 和 Gordon 的答案都有效,但如果您还想报告连接表中的其他字段,则会变得更加棘手,因为您需要将它们作为聚合字段包括在内或者也按他们分组。 Eg if you want your summary to also include the assessment they were given at their last appointment, GROUP BY is not the way to go.例如,如果您希望您的摘要还包括他们在上次约会时得到的评估,则 GROUP BY 不是 go 的方法。

A more versatile structure may be something like一个更通用的结构可能是这样的

SELECT Patient.ID, Patient.Name, Appointment.Date, Appointment.Assessment
FROM Patient INNER JOIN Appointment ON Patient.ID=Appointment.ID
WHERE Appointment.Date = (SELECT Max(Appointment.Date) FROM Appointment WHERE Appointment.ID = Patient.ID)
;

As an aside, you may want to think whether you should use a field named 'ID' to refer to the ID of another table (in this case, the Apppintment.ID field refers to the Patient.ID).另外,您可能想考虑是否应该使用名为“ID”的字段来引用另一个表的 ID(在本例中,Apppintment.ID 字段引用 Patient.ID)。 You may make your db more readable if you leave the 'ID' field as an identifier specific to that table and refer to that field in other tables as OtherTableID or similar, ie PatientID in this case.如果您将“ID”字段保留为特定于该表的标识符,并将其他表中的该字段称为 OtherTableID 或类似字段,即本例中的 PatientID,则可以使您的数据库更具可读性。 Or go all the way and include the name of the actual table in its own ID field.或者 go 并在其自己的 ID 字段中包含实际表的名称。

Edited after comment:评论后编辑:

Not quite sure why it would crash.不太清楚为什么会崩溃。 I just ran an equivalent query on 2 tables I have which are about 10,000 records each and it was pretty instanteneous.我只是对我拥有的 2 个表运行了一个等效查询,每个表大约有 10,000 条记录,而且非常即时。 Are your ID fields (i) unique numbers and (ii) indexed?您的 ID 字段是否 (i) 唯一编号和 (ii) 索引? Another structure which should do the same thing (adapted for your field names and assuming that there is an ID field in Appointments which is unique) would be something like:另一个应该做同样事情的结构(适应您的字段名称并假设在 Appointments 中有一个唯一的 ID 字段)将类似于:

SELECT PatientInfo.UID, PatientInfo.Name, Appointments.StartDateTime, Appointments.Assessment
FROM PatientInfo INNER JOIN Appointments ON PatientInfo_UID = Appointments.PatientFID
WHERE Appointments.ID = (SELECT TOP 1 ID FROM Appointments WHERE Appointments.PatientFID = PatientInfo_UID ORDER BY StartDateTime DESC)
;

But that is starting to look a bit contrived.但这开始看起来有点做作。 On my data they both produce the same result (as they should.) and are both almost instantaneous?根据我的数据,它们都产生相同的结果(它们应该如此。)并且几乎都是瞬时的? Always difficult to troubleshoot Access when it crashes - I guess you see no error codes or similar. Access 崩溃时总是很难对其进行故障排除 - 我想您看不到任何错误代码或类似代码。 Is this against a native?accdb database or another server?这是针对 native?accdb 数据库还是其他服务器?

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

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