J
J
jrborisov2016-06-14 23:55:04
SQL
jrborisov, 2016-06-14 23:55:04

How to group by two fields and select the maximum group from them?

i have a table with such fields
cf2a4a14ab1d427fbf11e613174e473c.png
they need to be grouped what i do :
SELECT id_new,id_user
FROM tbl_bookmarks
GROUP BY id_user,id_new i
get this result
71ab242aa6454768bbd201fdc29b9516.png
after which you need to select the maximum group, that is, the result should be
5 - 1
4 - 3
2 - 4
3 - 4
1 - 5
I hope it's possible)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2016-06-15
@k1lex

It is possible using RANK.

SELECT 
id_new,
id_user
FROM (
SELECT 
     id_new,
    id_user, 
   RANK() OVER (PARTITION BY id_user ORDER by id_new DESK ) AS RNK
FROM tbl_bookmarks
GROUP BY id_user,id_new 
) X WHERE RNK=1

In general, the most correct option is even simpler. You are doing the grouping. What's stopping you from doing this:
SELECT MAX(id_new),id_user
FROM tbl_bookmarks
GROUP BY id_user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question