GROUP BY忽略属性

Pew*_*Pew 9 sql sql-order-by oracle10g

例如,我有这个表:

itemgroup | 描述| 价格
A,a,10
A,b,12
A,c,14
B,g,11
B,h,16

现在我想在一个组中选择价格最高的行,如下所示:

A,c,14
B,h,16

SQL查询(功能齐全)让我接近这个:

SELECT itemgroup, MAX( price ) 
FROM table
GROUP BY itemgroup
Run Code Online (Sandbox Code Playgroud)

A,14
B,16

通过尝试这个我得到一个"不是GROUP BY表达式"-error:

SELECT itemgroup, description, MAX( price ) 
FROM table
GROUP BY itemgroup
Run Code Online (Sandbox Code Playgroud)

我需要像这样的伪查询:

SELECT itemgroup, IGNORE( description), MAX( price ) 
FROM table
GROUP BY itemgroup
Run Code Online (Sandbox Code Playgroud)

我希望我能解释一下我的小问题.

Ste*_*son 11

我通常最终做的事情如下:

SELECT t1.itemgroup, t1.description, t1.price
FROM table t1, 
    (SELECT itemgroup, MAX( price ) as price
     FROM table
     GROUP BY itemgroup) t2
WHERE t1.itemgroup = t2.itemgroup
AND t1.price = t2.price
Run Code Online (Sandbox Code Playgroud)


Bri*_*ach 5

使用分析功能:

SELECT itemgroup, description, price FROM 
    (select itemgroup, description, price, RANK() 
    OVER (PARTITION BY itemgroup ORDER BY max(price) DESC) as rank 
    FROM  group by itemgroup,description,price)a 
WHERE a.rank = 1
ORDER BY itemgroup;
Run Code Online (Sandbox Code Playgroud)

分析功能有很多功能-学习它们可以在很多情况下为您提供帮助。