0

Assuming I have the following table :

Table Files
 id
 size

In pseudo SQL I need this king of processing :

select id
where
size < S1 and (limit this clause to N1 results)
or
size > S2 and (limit this clause to N2 results)

I know Oracle defines rownum keyword to limit results.

But in my case, that does not work.

Do this require subselects ? How ? Is that possible to use multiple subselects ?

(This SQL would be generated by a java program with other where clauses, making the use of subselects difficult..)

Edit: I need to filter more than just different size.

SOLVED

SELECT * 
FROM Files
WHERE
    someField = 'stuff'
AND
    someOtherField = 'other stuff'
AND
(
    SELECT id FROM Files WHERE size <= S1 AND ROWNUM <= N1
    UNION
    SELECT id FROM Files WHERE size > S2 AND ROWNUM <= N2
)
3
  • Yes, N is a number of results Commented Mar 3, 2014 at 17:41
  • 1
    In your edit, why are you comparing size to two different numbers? I thought N1 and N2 were the number of results to return, not sizes. Commented Mar 3, 2014 at 18:09
  • You're right, fixed it. My example is not very nice, since the original problem is way complicated. Commented Mar 3, 2014 at 18:12

3 Answers 3

2

Use a UNION:

SELECT *
FROM (
    SELECT 'low' AS "which", "id"
    FROM Files
    WHERE "size" < S1)
WHERE ROWNUM <= N1

UNION

SELECT *
FROM (
    SELECT 'high' AS "which", "id"
    FROM Files
    WHERE "size" >= S1)
WHERE ROWNUM <= N2

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

@ObscurMoirage I've revised the answer to use Oracle syntax, and tested it at sqlfiddle.
@GordonLinoff indeed it was an Oracle SQL question. I guess i've lost everyone when using the term "limit" which is also a MySQL keyword. Thanks for your changes Barmar
2

Assuming the clauses are independent (which they are for your example), you can do this with union all:

select id
. . .
where size < S1 and rownum <= N1
union all
select id
. . .
where size > S1 and rownum <= N2

If you don't want to repeat the from clause (because it is complex for instance), you can use a CTE:

with t as (<blah blah blah>)
select *
from ((select id from t where size < S1 and rownum <= N1) union all
      (select id from t where size > S1 and rownum <= N2)
     ) x

2 Comments

Whats the difference between UNION and UNION ALL ?
UNION removes duplicates, UNION ALL leaves them in. They're equivalent in this case, since the IDs are from disjoint subsets of the data. But ALL should make the query more efficient, since it doesn't have to check for duplicates.
0

Solved using this query

SELECT * 
FROM Files
WHERE
    someField = 'stuff'
AND
    someOtherField = 'other stuff'
AND
(
    SELECT id FROM Files WHERE size <= N1  AND ROWNUM <= N1
    UNION
    SELECT id FROM Files WHERE size  > N2  AND ROWNUM <= N2
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.