The following query:
SELECT
year, id, rate
FROM h
WHERE year BETWEEN 2000 AND 2009
AND id IN (SELECT rid FROM table2)
GROUP BY id, year
ORDER BY id, rate DESC
yields:
year id rate
2006 p01 8
2003 p01 7.4
2008 p01 6.8
2001 p01 5.9
2007 p01 5.3
2009 p01 4.4
2002 p01 3.9
2004 p01 3.5
2005 p01 2.1
2000 p01 0.8
2001 p02 12.5
2004 p02 12.4
2002 p02 12.2
2003 p02 10.3
2000 p02 8.7
2006 p02 4.6
2007 p02 3.3
What I'd like is only the top 5 results for each id:
2006 p01 8
2003 p01 7.4
2008 p01 6.8
2001 p01 5.9
2007 p01 5.3
2001 p02 12.5
2004 p02 12.4
2002 p02 12.2
2003 p02 10.3
2000 p02 8.7
Is there a way to do this using some kind of LIMIT like modifier that works within the GROUP BY?
The subquery is almost identical to your query. Only change is adding
For me something like
works perfectly. No complicated query.
for example: get top 1 for each group
This requires a series of subqueries to rank the values, limit them, then perform the sum while grouping
Build the virtual columns(like RowID in Oracle)
table:
data:
SQL like this:
if delete the where clause in t3, it shows like this:
GET "TOP N Record" --> add the "rownum <=3" in where clause(the where-clause of t3);
CHOOSE "the year" --> add the "BETWEEN 2000 AND 2009" in where clause(the where-clause of t3);
for those like me that had queries time out. I made the below to use limits and anything else by a specific group.
it loops through a list of domains and then inserts only a limit of 200 each
Try this: