问题:
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购买的最大商品的id ( total )。 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
来源:oschina
链接:https://my.oschina.net/u/4428122/blog/4492109