在每个GROUP BY组中选择第一行?

我是研究僧i 提交于 2020-08-13 16:21:49

问题:

As the title suggests, I'd like to select the first row of each set of rows grouped with a GROUP BY . 顾名思义,我想选择以GROUP BY分组的每组行的第一行。

Specifically, if I've got a purchases table that looks like this: 具体来说,如果我有一个如下的purchases表:

SELECT * FROM purchases;

My Output: 我的输出:

id | customer | total
---+----------+------
 1 | Joe      | 5
 2 | Sally    | 3
 3 | Joe      | 2
 4 | Sally    | 1

I'd like to query for the id of the largest purchase ( total ) made by each customer . 我想查询每个customer购买的最大商品的idtotal )。 Something like this: 像这样:

SELECT FIRST(id), customer, FIRST(total)
FROM  purchases
GROUP BY customer
ORDER BY total DESC;

Expected Output: 预期产量:

FIRST(id) | customer | FIRST(total)
----------+----------+-------------
        1 | Joe      | 5
        2 | Sally    | 3

解决方案:

参考一: https://stackoom.com/question/FwhD/在每个GROUP-BY组中选择第一行
参考二: https://oldbug.net/q/FwhD/Select-first-row-in-each-GROUP-BY-group
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!