Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql select the first n rows per group

I've read this article: http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ and search for other questions

I have a table that is something like:

| table.id    | USER.id
----------------------------------------------
| 1           | 101
| 2           | 101
| 3           | 101
| 4           | 101 
| 5           | 101
| 6           | 101
| 7           | 101 
| 8           | 101
| 9           | 101 
| 10          | 101
| 11          | 102
| 12          | 102 
| 13          | 102
| 14          | 102
| 15          | 103 
| 16          | 103
| 17          | 103
| 18          | 103
| 19          | 103
| 20          | 103
| 21          | 103
| 22          | 103 
| 23          | 103
| 24          | 104
| 25          | 104 
| 26          | 104
| 27          | 104
| 28          | 104
| 29          | 104
| 30          | 105
| 31          | 105
| 32          | 105 
| 33          | 106
| 34          | 106

I'm trying to get the count of table.id grouped by user.id, and if the count of user.id is more than 7, only display the result as 7 (aka limiting the count results to 7).

In this example, the result should be:

| USER.id         | count of table.ID
----------------------------------------
| 101             | 7
| 102             | 4
| 103             | 7
| 104             | 6
| 105             | 3
| 106             | 2

I've tried:

SELECT USERid, COUNT(table.id)
FROM table
WHERE table.id IN (select top 7 table.id from table)
GROUP BY USERid

and

SELECT USERid, COUNT(table.id)
FROM table
WHERE (
    SELECT COUNT(table.ID) FROM table as t
    WHERE t.id = t.id AND t.USERid <= table.USERid
    ) <= 7
GROUP BY USERid
like image 566
Ken Avatar asked Oct 03 '13 20:10

Ken


People also ask

How do I select the first row in a group in SQL?

To do that, you can use the ROW_NUMBER() function. In OVER() , you specify the groups into which the rows should be divided ( PARTITION BY ) and the order in which the numbers should be assigned to the rows ( ORDER BY ).

How do I select top 10 rows in MySQL?

To select first 10 elements from a database using SQL ORDER BY clause with LIMIT 10. Insert some records in the table using insert command. Display all records from the table using select statement. Here is the alternate query to select first 10 elements.

How do you find the first value of each group?

groupby. nth() function is used to get the value corresponding the nth row for each group. To get the first value in a group, pass 0 as an argument to the nth() function.


2 Answers

You can simplify your query, and use LEAST function

SELECT USERid, LEAST(7, COUNT(*))
FROM table
GROUP BY USERid

from the question in your comment

SELECT SUM(countByUser) 
FROM
 (SELECT LEAST(7, COUNT(*)) as countByUser
  FROM table
  GROUP BY USERid) c

SqlFiddle

like image 151
Raphaël Althaus Avatar answered Sep 28 '22 10:09

Raphaël Althaus


SELECT userid,
CASE
  WHEN COUNT(*) > 7 THEN 7
  ELSE COUNT(*)
END AS Qty
FROM tbl
GROUP BY userid
like image 34
AgRizzo Avatar answered Sep 28 '22 09:09

AgRizzo