如何使用postgresSql或jpa获取具有所有列的group by的第一个匹配项?
我有这张桌子“测试” id是唯一的,但algo不是唯一的 我想要在输出中,所有列 我想按limite和unidade分组 我想要以下输出,我可以用它生成:如何使用postgresSql或jpa获取具有所有列的group by的第一个匹配项?,sql,postgresql,greatest-n-per-group,Sql,Postgresql,Greatest N Per Group,我有这张桌子“测试” id是唯一的,但algo不是唯一的 我想要在输出中,所有列 我想按limite和unidade分组 我想要以下输出,我可以用它生成: select limite, unidade from test group by limite, unidade limite, unidade 1 1 2 1 2 2 加上组第一次出现时的列id和algo id, limite, unidade, algo 1 1 1
select limite, unidade
from test
group by limite, unidade
limite, unidade
1 1
2 1
2 2
加上组第一次出现时的列id和algo
id, limite, unidade, algo
1 1 1 aegeg
3 2 1 gesrsrhbg
5 2 2 ftgjd\h
但是我不知道怎么做。对此有很多答案:哦,谢谢你,希皮奥尼,它成功了,我试图提高你的答案,但我没有这样做的声誉。现在我将尝试在jpa上复制它。
select a.id, a.limite, a.unidade, t.algo
from (
select
min(id) as id,
limite,
unidade
from test
group by
limite,
unidade
) a
left join test t on t.id = a.id
select a.id, a.limite, a.unidade, t.algo
from (
select
min(id) as id,
limite,
unidade
from test
group by
limite,
unidade
) a
left join test t on t.id = a.id