如何在MySQL中自我加入聚合函数的结果?

内森·卢特曼(Nathan Lutterman)

我有以下格式的数据:

Table: MountHeights

ID  | Height| Type  |  ItemCode     |  Context
--------------------------------------------
1   | 15    | max   | BD1896-1W     | exterior
2   | 12    | max   | BD1896-1W     | insect
3   | 18    | max   | BD1896-1W     | interior
4   | 13    | max   | BD14120-1W    | exterior
5   | 10    | max   | BD14120-1W    | insect
6   | 15    | max   | BD14120-1W    | interior

每个商品代码都有多行。

我试图找出一个办法让max(Height)每个ItemCodeType=“最大”,出于某种原因,我不能完全绕到我的头,我应该如何引用表本身。

我希望结果遵循以下原则:

Results:

 max(Height)| ItemCode  
---------------------------
 18         | BD1896-1W
 15         | BD14120-1W

我怎么做?

西尔科

此处无需自参考表。

SELECT `ItemCode`, MAX( `height` )
FROM `MountHeights`
WHERE `Type`= 'max'
GROUP BY `ItemCode`

小提琴的例子

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章