0

我在 MS Access 2010 中有 3 个表,如下所示:

表:设备

   id | device_id | Company | Version | Revision |
   -----------------------------------------------
   1  | dev_a     | Almaras | 1.5.1   | 0.2A     |
   2  | dev_b     | Enigma  | 1.5.1   | 0.2A     |
   3  | dev_c     | Almaras | 1.5.1   | 0.2C     |
*Field: device_id is Primary Key Unique String
*Field ID is just an auto-number column 

表:活动

   id | act_id | act_date   | act_type | act_note |
   ------------------------------------------------
    1 | dev_a  | 07/22/2013 | usb_axc  | ok       |
    2 | dev_a  | 07/23/2013 | usb_axe  | ok       | (LAST ROW for dev_a)
    3 | dev_c  | 07/22/2013 | usb_axc  | ok       | (LAST ROW for dev_c)
    4 | dev_b  | 07/21/2013 | usb_axc  | ok       | (LAST ROW for dev_b)
*Field: act_id contains device_id; NOT UNIQUE
*Field ID is just an auto-number column 

表:矩阵

  id | mat_id | tc    | ts | bat | cycles |
  -----------------------------------------
  1  | dev_a  | 2811  | 10 | 99  | 200    |
  2  | dev_a  | 2911  | 10 | 97  | 400    |
  3  | dev_a  | 3007  | 10 | 94  | 600    |
  4  | dev_a  | 3210  | 10 | 92  | 800    | (LAST ROW for dev_d)
  5  | dev_b  | 1100  | 5  | 98  | 100    |
  6  | dev_b  | 1300  | 8  | 93  | 200    |
  7  | dev_b  | 1411  | 11 | 90  | 300    | (LAST ROW for dev_b)
  8  | dev_c  | 4000  | 27 | 77  | 478    | (LAST ROW for dev_c)
*Field: mat_id contains device_id; NOT UNIQUE
*Field ID is just an auto-number column 

有没有办法查询表以获得如下所示的结果(来自设备的每个设备,并且仅添加了最后一行 [参见示例输出表] 从其他两个表中的每一个):

查询结果:

device_id | Company | act_date   | act_type | bat | cycles |
------------------------------------------------------------
device_a  | Almaras | 07/23/2013 | usb_axe  |  92 |   800  |
device_b  | Enigma  | 07/21/2013 | usb_axc  |  90 |   300  |
device_c  | Almaras | 07/22/2013 | usb_axc  |  77 |   478  |

有任何想法吗?提前感谢您阅读并帮助我:)

4

3 回答 3

3

我觉得是你想要的

SELECT  a.device_id, a.Company,
        b.act_date, b.act_type,
        c.bat, c.cycles
FROM    ((((devices AS a
        INNER JOIN activities AS b
            ON a.device_id = b.act_id)
        INNER JOIN matrix AS c
            ON  a.device_id = c.mat_id)
        INNER JOIN
        (
            SELECT  act_id, MAX(act_date) AS max_date
            FROM    activities
            GROUP   BY act_id
        ) AS d  ON b.act_id = d.act_id AND b.act_date = d.max_date)
        INNER JOIN
        (
            SELECT  mat_id, MAX(tc) AS max_tc
            FROM    matrix
            GROUP   BY mat_id
        ) AS e  ON c.mat_id = e.mat_id AND c.tc = e.max_tc)

子查询:de分别获取每个act_id.

于 2013-09-15T17:47:46.173 回答
0

您认为 Matrix 中的“最后”一行是什么?

你需要做类似的事情 WHERE act_date in (SELECT max(a.act_date) from activities a where a.mat_id=d.device_id GROUP BY a.mat_id)

以及与矩阵的连接类似的东西。

于 2013-09-15T17:48:54.610 回答
0

尝试

SELECT devices.device_id, devices.Company, activities.act_data, activities.act_type, matrix.bat, matrix.cycles
FROM devices
LEFT JOIN activities
ON devices.device_id = activities.act_id
LEFT JOIN matrix
ON devices.device_id = matrix.mat_id;
于 2013-09-15T17:42:50.840 回答