使用join访问sql聚合

时间:2016-10-23 18:03:56

标签: ms-access

我有一张表如下:

tableA

+----+------+------+------+
| ID | fldA | fldB | fldC |
+----+------+------+------+
|  1 |  123 | xx1  |    1 |
|  1 |  124 | xx3  |    5 |
|  2 |  543 | xx5  |    3 |
|  2 |  465 | px3  |    2 |
|  3 |   73 | ox6  |    7 |
|  3 |   55 | ox7  |   24 |
+----+------+------+------+

我想通过仅选择那些在字段ID方面具有最高fldC值的记录来减少此表,以便我们最终得到:

+----+------+------+------+
| ID | fldA | fldB | fldC |
+----+------+------+------+
|  1 |  124 | xx3  |    5 |
|  2 |  543 | xx5  |    3 |
|  3 |   55 | ox7  |   24 |
+----+------+------+------+

我可以减少:

SELECT ID, Max(fldC) AS MaxC
FROM tableA
GROUP BY ID;

但是我在尝试包含字段fldA和fldB时遇到了问题。我确定答案在于内部加入和一些别名但却无法使用。

1 个答案:

答案 0 :(得分:0)

类似的东西:

 SELECT a.id, fldA, fldC,  fldC
   from tableA a
        join
      (SELECT ID, Max(fldC) AS MaxC 
         FROM tableA 
        GROUP BY ID) b 
       On a.fldc=b.maxC and a.id=b.id;