Good News Everybody!
The new search engine is LIVE!
Please report any problems to david (at) midrange.com.
|
CRPence on Friday, 21.3.2014 21:51 wrote:
On 21-Mar-2014 12:41 -0700, Stone, Joel wrote:
I would like to select 5 rows (of thousands) where field1 = 'A'
and union that with 5 rows where field1 = 'B'
How can I accomplish this with SQL?
I have tried variations of
select * from file1
where field1 = 'A'
FETCH FIRST 5 ROWS ONLY
union
select * from file1
where field1 = 'B'
FETCH FIRST 5 ROWS ONLY
To use the FETCH ... ONLY clause in that context, the subquery
must be a derived table expression; e.g. a Nested Table Expression
(NTE). <<SNIP>>
This rule was removed with release 6.1, i.e. FETCH FIRST x ROWS
and ORDER BY can be directly used within a Sub-Select. But the
Sub-Select must be enclosed in parentheses.
The following statement works on Release 7.1 without any problem:
( Select *
from MyTable
Where MyField1 = 'A'
Order By MyField2 Desc
Fetch First 5 rows only
)
Union
( Select *
From MyTable
Where MyField1 = 'B'
Order By MyField2 Desc
Fetch First 5 Rows Only
)
;
And even the following statement works without any problems:
( Select *
from MyTable
Where MyField1 = 'A'
Order By MyField2 Desc
Fetch First 5 rows only
)
Union
( Select *
From MyTable
Where MyField1 = 'B'
Order By MyField2 Desc
Fetch First 5 Rows Only
)
Order By MyField2, MyField1
;
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2026 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.