Subject Re: FIRST/SKIP in a Subquery
Author Alexander V.Nevsky
--- In firebird-support@yahoogroups.com, "jrodenhi" <jack@r...> wrote:
> This question duplicates one I sent to the Atkins server because my
> posting there was not immediately approved and I don't know that it
> will be. Any help offered on this issue would be much appreciated.
>
> If I run this query against the example Employee database:
>
> SELECT FIRST 5 Emp_No
> FROM Employee
> ORDER BY Last_Name
>
> I get, as I would expect, 5 records in my result set. But, if make
> this query a subquery as in this:
>
> SELECT Emp_No, New_Salary
> FROM Salary_History
> WHERE Emp_No IN (
> SELECT FIRST 5 Emp_No
> FROM Employee
> ORDER BY Last_Name
> )
> I get a gazillion records back. Is this a bug. How would I get the
> Salary_History back just for those first 5 employees?
>
> Also, if I modify the first line of the subquery to read:
> SELECT FIRST 5 SKIP 5 Emp_No
> I get nothing back at all. This is not helpful. Do I have my
syntax
> wrong? Thanks for your help.

Jack, yes, it is known bug. If I right understand result which you
want (all history for first 5 persons selected by ordered last name),
it can be retrieved by stored procedure

Create SP_Get5
Returns(Emp_No <type>, New_Salary <type>)
As
Begin
For SELECT FIRST 5 Emp_No
FROM Employee
ORDER BY Last_Name
Into :Emp_No
Do
For SELECT New_Salary
FROM Salary_History
WHERE Emp_No=:Emp_No
Into :New_Salary
Do
Suspend;
End

Best regards,
Alexander.