Hi Robert,
SQL has a LIMIT keyword. I have used it to take the top 'x' entries of a
large table , so for example:
SELECT * from table LIMIT 20

You should be able to use it in your query like so:
select div, team, max(wins) from teams group by div limit 5;

-deech

On Thu, Jan 8, 2009 at 12:06 AM, Robert Citek <robert.ci...@gmail.com>wrote:

> How can I construction a SQL query to pick the top three (3) items in a
> group?
>
> I have a list of sports teams which are grouped into divisions, say A,
> B, C, D, etc.  At the end of the season I would like to get a list of
> the top three teams (those with the most wins) in each division.  If I
> wanted the best team from each division, I could write this:
>
> select div, team, max(wins) from teams group by div ;
>
> Unfortunately, there's no option to max to specify more than one item,
> e.g. max(wins,3) to specify the top 3.
>
> In pseudocode, I want to do something similar to this:
>
> for $i in (select div from teams) {
>  select div, team, wins from teams where div=$i order by wins desc limit 3
> ;
> }
>
> Is there a way to do the equivalent using only SQL?
>
> Thanks in advance for any pointers.
>
> Regards,
> - Robert
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to