N
N
NubasLol2019-04-24 18:22:04
PostgreSQL
NubasLol, 2019-04-24 18:22:04

Is it possible to get the first order from a group?

For example, there is a table with an auto-increment id and a group_key column. And let's say in the table 2 records have group_key equal to 4 and 3 records have group_key equal to 44, and I need to get the first 2 of this group, like
id = 1, group_key = 4
id = 32, group_key = 44

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
UndineS, 2019-04-24
@NubasLol

SELECT min(id), group_key
FROM table_name
GROUP BY group_key
where table_name is the name of the table in the database

K
Konstantin Tsvetkov, 2019-04-24
@tsklab

SELECT MIN(id), group_key FROM your_table GROUP BY group_key

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question