访问:根据记录中的最近日期进行分组(嵌套查询)

时间:2015-09-17 02:21:21

标签: sql ms-access

此查询位于下表

SELECT ID, 
       Value, 
       As_of 
FROM Table a 
INNER JOIN (
    SELECT ID, 
           MAX(As_of) AS As_of 
    FROM Table 
    GROUP BY ID
) b 
ON a.ID=b.ID 
AND a.As_of = b.As_of

表格

ID      Value  As_of
1173    156    20090601
1173    173    20081201
1173    307    20080901
1173    305    20080601
127     209    20090301
127     103    20081201
127     113    20080901
127     113    20080601
1271    166    20090201
1271    172    20081201
1271    170    20080901
1271    180    20080601

使其看起来像这样

Result
ID      Value    As_of
1173    156      20090601
127     209      20090301
1271    166      20090201

它获取每个ID的As_of的MAX并返回...(来自stackoverflow上的另一个Question

我基本上有相同的表,但不是“物理表”,而是查询结果:

查询:

SELECT tblClassificationHistory.inmateID, 
       tblClassificationHistory.classificationID
FROM tblinmate 
INNER JOIN tblClassificationHistory 
ON tblinmate.inmateID = tblClassificationHistory.inmateID
GROUP BY tblClassificationHistory.inmateID, 
         tblClassificationHistory.classificationID,                
         tblClassificationHistory.reclassificationDate
ORDER BY tblClassificationHistory.inmateID;

产生此结果:

inmateID    classificationID    reclassificationDate
2              3                       9/22/2015
2              4                       9/13/2015
2              8                       9/8/2015
3              8                       9/13/2015
4              4                       9/15/2015
4              8                       6/16/2015
5              3                       9/15/2015
5              4                       9/16/2015
5              8                       7/7/2015
6              8                       9/14/2015
7              7                       9/13/2015
8              3                       8/4/2015
8              8                       6/1/2015
9              3                       9/15/2015
10              6                       9/13/2015
11              3                       9/13/2015
12              3                       8/3/2015
12              4                       9/9/2015
12              6                       9/13/2015
12              8                       7/6/2015
13              8                       9/13/2015
14              8                       9/13/2015
15              4                       9/13/2015
16              4                       9/13/2015
17              8                       9/14/2015
18              8                       9/14/2015
19              3                       9/15/2015

我想在我的脑海中使用“他的”查询,但几个小时后我就不能......我猜它是一个子查询......但我不能为我的生活得到正确的语法......

结果我之后:每个inmateID的上次日期的分类ID

inmateID    classificationID    reclassificationDate
2           3                   9/22/2015
3           8                   9/13/2015
4           4                   9/15/2015
5           4                   9/16/2015
6           8                   9/14/2015
7           7                   9/13/2015
8           3                   8/4/2015
9           3                   9/15/2015
10          6                   9/13/2015
11          3                   9/13/2015
12          6                   9/13/2015
13          8                   9/13/2015
14          8                   9/13/2015
15          4                   9/13/2015
16          4                   9/13/2015
17          8                   9/14/2015
18          8                   9/14/2015
19          3                   9/15/2015

1 个答案:

答案 0 :(得分:2)

SELECT a.inmateID,
       a.classificationID,
       b.max_date
FROM (
    SELECT tblClassificationHistory.inmateID, 
           tblClassificationHistory.classificationID,                
           tblClassificationHistory.reclassificationDate
    FROM tblinmate
    INNER JOIN tblClassificationHistory
    ON tblinmate.inmateID = tblClassificationHistory.inmateID 
) a
INNER JOIN (
    SELECT tblClassificationHistory.inmateID, 
           MAX(tblClassificationHistory.reclassificationDate) as max_date
    FROM tblinmate
    INNER JOIN tblClassificationHistory
    ON tblinmate.inmateID = tblClassificationHistory.inmateID
    GROUP BY tblClassificationHistory.inmateID  
) b
ON a.inmateID = b.inmateID
AND a.reclassificationDate = b.max_date
ORDER BY a.inmateID;

您可以将其细分为两个子查询(ab)。在b中,我们会在表格中找到MAX日期,不包括classificationID,只能通过inmateID进行分组。我们最后得到一张这样的表:

+----------+-----------+
| inmateID | max_date  |
+----------+-----------+
| 2        | 9/22/2015 |
| 3        | 9/13/2015 |
| 4        | 9/15/2015 |
| 5        | 9/16/2015 |
| 6        | 9/14/2015 |
| 7        | 9/13/2015 |
| ...      | ...       |
+----------+-----------+

a中,我们只是抓取未聚合的值,包括此复发的分类ID:

+----------+------------------+----------------------+
| inmateID | classificationID | reclassificationDate |
+----------+------------------+----------------------+
| 2        | 3                | 9/22/2015            |
| 2        | 4                | 9/13/2015            |
| 2        | 8                | 9/15/2015            |
| 3        | 8                | 9/16/2015            |
| 4        | 4                | 9/14/2015            |
| 4        | 8                | 9/13/2015            |
| ...      | ...              | ...                  |
+----------+------------------+----------------------+

我们加入两个子查询,在a中查找行,其中reclassificationDate等于MAX日期,刚刚在b中计算。然后,我们可以从那些连接的行中获取适当的分类ID来构建我们的最终表格。