Subject Re: [firebird-support] Top N Orders
Author Svein Erling Tysvaer
SELECT DISTINCT GroupColumn, Sales
FROM ATABLE A
WHERE NOT EXISTS(
SELECT * FROM ATABLE B
JOIN ATABLE C ON C.GroupColumn = B.GroupColumn
WHERE C.Sales > B.Sales
AND B.Sales > A.Sales)

This should get you the top two sales for each group. Note that if you want
additional information and there is a possibility of more several records
having the same value, then you risk getting more than two records (though
if you have a primary key it should be possible to circumvent).

Set

At 10:29 16.07.2003 +0100, you wrote:
>Hi All,
>Can I've a top N query where I can get the top N data for a particular
>group. For example :
>I've a sales tables which contain data for each & every sale. I want ot
>have Top 2 sales of each month.
>The 'Select First X.....' clause seems to be only for the result set & not
>the group.