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

  • SEARCH
  • Home
  • 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 8723257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:35:20+00:00 2026-06-13T07:35:20+00:00

I am currently running the following SQL fiddle and am trying to get the

  • 0

I am currently running the following SQL fiddle

and am trying to get the following to be true:

I only want the set of cats that have had a last_checked value of some arbitrary time and are sick. for the sake of discussion lets make it now minus 12 hours. (sysdate - Interval '12' hour). What I am getting back is the cat that was last_checked 11 hours ago, but that cat has a more recent status of now where he is no longer sick. How can I construct a query in such a way that I ignore the old sick entry and only concern myself with the new healthy status? By that I mean ignore this result because the cat is no longer sick.

Question

So… do you want the most recent status regardless, the most recent
status of only those cats that have been sick in the given timeframe,
or only healthy cats?

I only want a status to be returned to me if it is the most recent (max) AND the status is also sick. In the fiddle it will show an example what I don’t want to happen, it will return the status of sick with the timestamp of 11 hours ago but there is a more recent one.

UPDATE

The following function:

select cat_id, last_checked, sick
from
(
    select cat_id, last_checked, sick,
             ROW_NUMBER() over (partition by cat_id order by last_checked desc) rn
    from cats
    where last_checked >= sysdate- INTERVAL '12' hour
) v
where rn = 1
and sick = 1;

provided by @podiluska only retrieves the first result. I need it to be on a cat by cat basis.

Second Update

The solution provided by Podiluska:

select cat_id, last_checked, sick
from
(
    select cat_id, last_checked, sick,
             ROW_NUMBER() over (partition by cat_id order by last_checked desc) rn
    from cats
    where last_checked >= sysdate- INTERVAL '12' hour
) v
where rn = 1;

will work on small data sets. The accepted answer (the one I used) will work most effectively on large data sets.

  • 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-06-13T07:35:21+00:00Added an answer on June 13, 2026 at 7:35 am

    Ok so in case someone else runs into this same scenario that I did, you do not want to use an analytic function especially when your data is > 500 million records as mine is. What you want to do is unwrap the analytic query into a sql statement that has the following form:

    select colA,colB,...  
    from  
    (  
         select col1,col2,...
         from tables  
         where ...
    )  results  
    where ...
    group by colA,colB
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get our application that is currently running on glassfish 2.1
I have the following problem: I have a form in site/banen (currently local running
I am currently running the following SQL statement on my MySQL database: INSERT INTO
I have a SQL Server that's currently x86, and I'd like to upgrade it
I'm currently running the following query: SELECT * from people WHERE id = 4;
Currently I do the to following clear all processes running on port lsof -i
I am currently running into an issue trying to create delegates from MethodInfo .
I'm currently running some SQL which uses an IN expression to match against multiple
I am currently running a PHP script that queries the database and then sends
I have a small Android app and currently I am firing a sql statement

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.