Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 694373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:51:00+00:00 2026-05-14T02:51:00+00:00

I have an big problem with an SQL Statement in Oracle. I want to

  • 0

I have an big problem with an SQL Statement in Oracle. I want to select the TOP 10 Records ordered by STORAGE_DB which aren’t in a list from an other select statement.

This one works fine for all records:

SELECT DISTINCT 
  APP_ID, 
  NAME, 
  STORAGE_GB, 
  HISTORY_CREATED, 
  TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') AS HISTORY_DATE  
  FROM HISTORY WHERE 
      STORAGE_GB IS NOT NULL AND 
        APP_ID NOT IN (SELECT APP_ID
                       FROM HISTORY
                        WHERE TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') = '06.02.2009') 

But when I am adding

AND ROWNUM <= 10
ORDER BY STORAGE_GB DESC

I’m getting some kind of “random” Records. I think because the limit takes in place before the order.

Does someone has an good solution? The other problem: This query is realy slow (10k+ records)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:51:00+00:00Added an answer on May 14, 2026 at 2:51 am

    You’ll need to put your current query in subquery as below :

    SELECT * FROM (
      SELECT DISTINCT 
      APP_ID, 
      NAME, 
      STORAGE_GB, 
      HISTORY_CREATED, 
      TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') AS HISTORY_DATE  
      FROM HISTORY WHERE 
        STORAGE_GB IS NOT NULL AND 
          APP_ID NOT IN (SELECT APP_ID FROM HISTORY WHERE TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') ='06.02.2009')
      ORDER BY STORAGE_GB DESC )
    WHERE ROWNUM <= 10
    

    Oracle applies rownum to the result after it has been returned.
    You need to filter the result after it has been returned, so a subquery is required. You can also use RANK() function to get Top-N results.

    For performance try using NOT EXISTS in place of NOT IN. See this for more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty big SQL statement which returns a list of id's. I
I have a big problem. I had a configuration which worked properly till today.
I have a big problem. I want to extract text from html table that
I have a big (1.9 GB) XML file which has data I want to
In my C# program I have a big problem: I add an SQL server
I have a big SQL Server database I want to connect to and my
I have very big problem in my SQL query and i don't know how
We are fighting this problem: We have a big table in SQL Server 2008
I have big problem when I am trying to deploy my app over clickonce.
I'm using editor from Kendo UI, so I have big problem. I don't know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.