0

Let's say I have the following query

Select user_id, item_id
from some_table

And your result is this

10,20
10,21
10,22
10,23
10,24
11,30
11,31
11,32
11,33
11,34

What I really want is to only have 2 friend per each user, the following would be the desired output

10,20
10,21
11,30
11,31

How do you modify the query above to work this way without nested queries?

Anathorn
  • 341
  • 3
  • 10
  • 1
    Try this sql fidddle : http://www.sqlfiddle.com/#!9/dda15b/2/0 Following is the query: SELECT user_id, item_id FROM some_table s WHERE ( SELECT COUNT(*) FROM some_table f WHERE f.user_id = s.user_id AND f.item_id <= s.item_id ) <= 2; – Suryansh Singh Sep 10 '21 at 17:28
  • Is this doable without nested query? – Anathorn Sep 10 '21 at 19:13
  • check out this question very similar to yours, see if you find something : https://stackoverflow.com/q/15969614/15387341 – Suryansh Singh Sep 10 '21 at 23:31

0 Answers0