Subject AW: [firebird-support] Select last n rows
Author Alexander Gräf
> -----Ursprüngliche Nachricht-----
> Von: firebird-support@yahoogroups.com
> [mailto:firebird-support@yahoogroups.com] Im Auftrag von guru
> Gesendet: Donnerstag, 21. April 2005 20:10
> An: Firebird Support
> Betreff: [firebird-support] Select last n rows
>
>
> Hi,
>
> I'm trying to get last n rows from the query. It looks like
>
> select degree, degree_type
> from ga_degree
> where degree is not NULL
> order by entry_date
>
> Lets say we have 100 rows in a table. All have different
> "entry_date".
> I want to get "last n rows" from ga_degree.
> Can not change order by clause because;
> the result of a sp ( yes this is a sp ) is a string and that is :
> '0056730236'. ( something like that )
> Every digit represents a "degree" which was retrieved from guery.
> Most left is smaller date's degree. And most right is the
> last degree entry..
> That why can't use "select first n ..... order by entry_date desc".
>
> Well, with my poor English, thats all I can tell :)
>
> Thanks for help...
>

Now thats an easy one:

The inner select orders by date and only selects the PK-value from the first 100 rows, and the outer is a normal select with the contraint:

SELECT * FROM MagicSP WHERE PK_Magic IN
(SELECT PK_Magic FIRST 100 FROM Table ORDER BY Date);

Cheers (do not talk about performance)

PS: Why not simply change the SP to only include the last 100 entries?