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 7989417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:44:49+00:00 2026-06-04T12:44:49+00:00

I have a sample data like this ID DATE TIME STATUS ——————————————— A 01-01-2000

  • 0

I have a sample data like this

ID     DATE           TIME     STATUS 
---------------------------------------------
A      01-01-2000     0900     ACTIVE 
A      05-02-2000     1000     INACTIVE 
A      01-07-2000     1300     ACTIVE 
B      01-05-2005     1000     ACTIVE 
B      01-08-2007     1050     ACTIVE
C      01-01-2010     0900     ACTIVE 
C      01-07-2010     1900     INACTIVE

From the above data set, if we only focus on ID='A' we note that A was initally active, then became inactive on 05-02-2000 and then it was inactive until 01-07-2000.

Which means that A was inactive from 05-Feb-2000 to 01-July-2000.

My questions are:

  1. if I execute a query with (ID=A, Date=01-04-2000) it should give me

    A      05-02-2000     1000     INACTIVE 
    

    because since that date is not available in that data set, it should search for the previous one and print that

  2. Also, if my condition is (ID=A, Date=01-07-2000) it should not only print the value which is present in the table, but also print a previous value

    A      05-02-2000     1000     INACTIVE 
    A      01-07-2000     1300     ACTIVE 
    

I would really appreciate if any one can assist me solve this query. I am trying my best to solve this.

Thank you every one.

Any take on this?

Afaq

  • 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-04T12:44:50+00:00Added an answer on June 4, 2026 at 12:44 pm

    Something like the following should work:

    SELECT ID, Date, Time, Status
     from (select ID, Date, Time, Status, row_number() over (order by Date) Ranking
            from MyTable
            where ID = @SearchId
             and Date <= @SearchDate) xx
     where Ranking < 3
     order by Date, Time
    

    This will return at most two rows. Its not clear if you are using Date and Time datatyped columns, or if you are actually using reserved words as column names, so you’ll have to fuss with that. (I left out Time, but you could easily add that to the various orderings and filterings.)


    Given the revised criteria, it gets a bit trickier, as the inclusion or exclusion of a row depends upon the value returned in a different row. Here, the “second” row, if there are two or more rows, is included only if the “first” row equals a particular value. The standard way to do this is to query the data to get the max value, then query it again while referencing the result of the first set.

    However, you can do a lot of screwy things with row_number. Work on this:

    SELECT ID, Date, Time, Status
     from (select
              ID, Date, Time, Status
             ,row_number() over (partition by case when Date = @SearchDate then 0 else 1 end
                                 order by     case when Date = @SearchDate then 0 else 1 end
                                             ,Date) Ranking
            from MyTable
            where ID = @SearchId
             and Date <= @SearchDate) xx
     where Ranking = 1
     order by Date, Time
    

    You’ll have to resolve the date/time issue, since this only works against dates.

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

Sidebar

Related Questions

If I have data like this DF <- data.frame( date = seq(Sys.Date()-1000, len=1000, by=1
I have data like this. Following is the sample data. alt text http://a.imageshack.us/img695/441/custoemrids.png I
I have some data in .cvs. I would like to make a simple barplot
I have the following sample data: Id Name Quantity 1 Red 1 2 Red
I have the following sample of data to insert into tables(from parent to child,
1I have the following two tables (sample data) and need to be able to
I have posted my class below and with sample data. I need to pass
Here is the sample data for test table I have [childId] [parentId] [present] 1
I have a Sample Code where I'm trying to read data from an xml
I have a python script which outputs lots of data, sample is as below.

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.