Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择每个值的最大值()_Sql_Mariadb_Greatest N Per Group - Fatal编程技术网

Sql 选择每个值的最大值()

Sql 选择每个值的最大值(),sql,mariadb,greatest-n-per-group,Sql,Mariadb,Greatest N Per Group,我有一张这样的桌子: name price class a 10 x a 20 y a 15 z b 40 y b 35 z c 5 x c 10 y c 15 z c 12 w name price class a 20 y b 40 y c 15

我有一张这样的桌子:

name  price  class
a       10     x
a       20     y
a       15     z
b       40     y
b       35     z
c       5      x
c       10     y
c       15     z
c       12     w
name  price  class
 a     20      y
 b     40      y 
 c     15      z
我想得到每一个名字,这是最高的价格和它所属的阶级。大概是这样的:

name  price  class
a       10     x
a       20     y
a       15     z
b       40     y
b       35     z
c       5      x
c       10     y
c       15     z
c       12     w
name  price  class
 a     20      y
 b     40      y 
 c     15      z
我试过这个:

SELECT name, max(price), class 
from t 
GROUP by name
但这让我上错了课。我明白了:

name  price  class
 a     20      x
 b     40      y 
 c     15      x

你能告诉我我做错了什么吗?

这应该是你想要的:

SELECT T.*
FROM T
    INNER JOIN (SELECT Name,
                    MAX(price) maxPrice
                FROM t
                GROUP BY Name) tMax on T.Name = tMax.Name
                                           AND T.Price = tMax.maxPrice

如果有两个类对特定名称具有相同的最大值,您希望输出结果是什么?@augustinatossi结果是正确的,但您误解了它。你确实得到了那个人的最大值,但你要找的是:给我一整行,其中价格等于那个名字的最大价格。如以下Radu给出的答案所示。