Group-wise Maximum of a Certain Column

后端 未结 5 1893
长情又很酷
长情又很酷 2020-12-19 16:24

I\'ve got the table:

SELECT * FROM shop;

+---------+--------+------
| article | dealer | price
+---------+--------+------
|    0001 | A      |  3.45
|    00         


        
5条回答
  •  渐次进展
    2020-12-19 17:28

    This does not work, because if you use group by, you can not use the individual fields of the original rows (except for the field you are grouping on). The correct way to do this, is to make an inner/nested query to select the dealer, suck as this (I haven't tested it, so it might be slightly off):

    SELECT article, MAX(price) as maxPrice, (SELECT dealer FROM shop AS s2 WHERE s2.article = s1.article AND s2.price = maxPrice) AS expensiveDealer FROM shop AS s1 GROUP BY(article);

提交回复
热议问题