S
S
Sergey Rybakov2020-06-04 21:56:51
MySQL
Sergey Rybakov, 2020-06-04 21:56:51

How to get all columns when grouping by key while keeping the maximum of the cells in the value?

There is a tabel table with columns userid, type, value, param2.
Filled like this:
userid | type____| value | param2.
1______| delete_| 2_____| name
1______| add____| 5______| name
2______| add____| 3______| name2
3______| delete_| 3______| name3
4______| update_| 5______| name4
4______| add____| 8______| name4
5______| update_| 2______| name5

Expected fetch result:
[{
type: delete, value: 3, param2: name3, userid: 3
}, {
type: add, value: 8, param2: name4, userid: 4
}, {
type: update, value: 5 , param2: name4, userid: 4
}, ].

I thought to use distinc, but when using it, I cannot specify the entire line for sampling. When trying to group by - a random value is selected for the param2, userid field

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Derepko, 2020-06-04
@kyctarnik

SELECT * FROM {table}
WHERE id in (SELECT max(id) FROM {table} GROUP BY type)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question