SQL中的最新日期,带有四个链接表

时间:2016-05-08 16:35:28

标签: sql access

我尝试更新最初在Access 2003中编写的PAT测试数据库。我使用HTML样式和activeX连接从访问数据库中获取数据。我是新编码,基本上通过谷歌找到我的大部分答案。目前,我对如何编写查找设备上次测试日期的SQL查询感到困惑。数据库中的信息位于4个数据库表中。 (抱歉格式化表格。)

Appliance_Details

+-------+------------------+----------+------------+
| PATID | ShortDescription | Location | Department |
+-------+------------------+----------+------------+
| FD110 |                1 | Room 125 |          1 |
| FD113 |                2 | Room 125 |          1 |
| FD114 |                3 | Room 125 |          1 |
+-------+------------------+----------+------------+

Appliance_Description

+--------+----------------+
| Number | AppDescription |
+--------+----------------+
|      1 | Microwave      |
|      2 | Food Steamer   |
|      3 | Smoothie Maker |
+--------+----------------+

Department_List

+--------+-------------------+
| Number |   Department      |
+--------+-------------------+
|      1 | Food Technology   |
|      2 | Art               |
|      3 | Humanities        |
+--------+-------------------+

Test_Results

+-------+---------+----------+
| PATID | AppPass | TestDate |
+-------+---------+----------+
| FD110 | Pass    | 23/04/15 |
| FD110 | Pass    | 04/04/14 |
| FD110 | Pass    | 10/04/13 |
| FD113 | Pass    | 23/04/15 |
| FD113 | Fail    | 04/04/14 |
| FD113 | Pass    | 10/04/13 |
| FD114 | Pass    | 23/04/15 |
| FD114 | Pass    | 04/04/14 |
| FD114 | Pass    | 10/04/13 |
+-------+---------+----------+

到目前为止我的SQL查询是: -

SELECT l.department, d.PATID AS Appliance, AppDescription As Type, Location, AppPass AS Result, TestDate 
FROM (Department_List AS l INNER JOIN (Appliance_Description AS a INNER JOIN Appliance_Details AS d ON a.Number = d.ShortDescription) ON l.Number = d.Department) INNER JOIN Test_Results AS t
ON d.PATID = t.PATID 
ORDER BY l.department, d.PATID

给出了结果

+------------+-------+----------------+----------+--------+----------+
| Department | PATID |      Type      | Location | Result | TestDate |
+------------+-------+----------------+----------+--------+----------+
| Design     | FD110 | Microwave      | Room 125 | Pass   | 23/04/15 |
| Design     | FD110 | Microwave      | Room 125 | Pass   | 04/04/14 |
| Design     | FD110 | Microwave      | Room 125 | Pass   | 10/04/13 |
| Design     | FD113 | Food Steamer   | Room 125 | Pass   | 23/04/15 |
| Design     | FD113 | Food Steamer   | Room 125 | Fail   | 04/04/14 |
| Design     | FD113 | Food Steamer   | Room 125 | Pass   | 10/04/13 |
| Design     | FD114 | Smoothie Maker | Room 125 | Pass   | 23/04/15 |
| Design     | FD114 | Smoothie Maker | Room 125 | Pass   | 04/04/14 |
| Design     | FD114 | Smoothie Maker | Room 125 | Pass   | 10/04/13 |
+------------+-------+----------------+----------+--------+----------+

我希望SQL查询做的是返回最近的日期。我知道我必须使用max函数,我有这个SQL查询,但我不确定如何将它加入上面的SQL查询语句。

SELECT MAX(t.TestDate) AS LastTestDate, t.PATID FROM Test_Results AS t GROUP BY t.PATID 

这应该给我如下所示的结果

+-------------+---------+------------------+------------+----------+-----------------+
| Department  |  PATID  |       Type       |  Location  |  Result  |  LastTestDate   |
+-------------+---------+------------------+------------+----------+-----------------+
| Design      |  FD110  |  Microwave       |  Room 125  |  Pass    |  23/04/15       |
| Design      |  FD113  |  Food Steamer    |  Room 125  |  Pass    |  23/04/15       |
| Design      |  FD114  |  Smoothie Maker  |  Room 125  |  Pass    |  23/04/15       |
+-------------+---------+------------------+------------+----------+-----------------+

1 个答案:

答案 0 :(得分:0)

您需要将MAXGROUP BY一起使用(请参阅下面的示例)。 在SELECT / GROUP BY中添加或删除列将允许MAX在不同的级别操作,例如,特定地点的最后考试日期等。

SELECT l.department, 
  d.PATID AS Appliance, 
  AppDescription As Type, 
  Location, 
  AppPass AS Result, 
  MAX(t.TestDate) AS LastTestDate 
FROM (Department_List AS l INNER JOIN (Appliance_Description AS a INNER JOIN Appliance_Details AS d ON a.Number = d.ShortDescription) ON l.Number = d.Department) 
INNER JOIN Test_Results AS t ON d.PATID = t.PATID 
GROUP BY l.department, d.PATID, AppDescription, Location, AppPass
ORDER BY l.department, d.PATID