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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:34:03+00:00 2026-05-24T09:34:03+00:00

I am struggling to write a query based on the table below that should

  • 0

I am struggling to write a query based on the table below that should get Bad records for a Location (where BadRec =1) and also previous 3 records(Max) on that Location including bad records;

TranDate                Location BadRec
======================= ======== ======
2010-08-25 00:00:00.000  STN-2     0
2010-08-26 00:00:00.000  STN-2     1
2010-08-27 00:00:00.000  STN-1     1
2010-08-28 00:00:00.000  STN-1     0
2010-08-28 00:00:00.000  STN-2     1
2010-08-29 00:00:00.000  STN-1     0
2010-08-30 00:00:00.000  STN-1     1
2010-08-31 00:00:00.000  STN-1     0
2010-09-01 00:00:00.000  STN-1     0

Here is the expected out Put;

TranDate                Location BadRec comments
======================= ======== ====== ========
2010-08-30 00:00:00.000  STN-1     1    <- Bad record on STN-1
2010-08-29 00:00:00.000  STN-1     0    <- First Previous record
2010-08-28 00:00:00.000  STN-1     0    <- Second Previous record
2010-08-27 00:00:00.000  STN-1     1    <- Third Previous record (Previous include bad rec)
2010-08-28 00:00:00.000  STN-2     1    <- Bad record on STN-2
2010-08-26 00:00:00.000  STN-2     1    
2010-08-25 00:00:00.000  STN-2     0    
2010-08-27 00:00:00.000  STN-1     1    <- Bad record on STN-1,No previous record for this 
2010-08-26 00:00:00.000  STN-2     1    <- Bad record on STN-2
2010-08-25 00:00:00.000  STN-2     0    

Is this possible to write this using a single query?? Using CTE?? Over (partition…)?? Any help is well appreciated.
Note: I am on a SQL 2005 machine;

  • 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-24T09:34:05+00:00Added an answer on May 24, 2026 at 9:34 am

    Does this do what you want?

    SELECT TranDate, Location, BadRec
    FROM
        (
        SELECT pr.TranDate, pr.Location, pr.BadRec,
            ROW_NUMBER() OVER (PARTITION BY r.TranDate ORDER BY pr.TranDate DESC) AS SEQUENCE
        FROM Records r
        JOIN Records pr ON r.Location = pr.Location
                        AND r.TranDate >= pr.TranDate
        WHERE r.BadRec = 1
        ) x
    WHERE SEQUENCE <= 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am struggling to write a query to result in the following records. I
Hi i'm struggling to write a particular MySQL Join Query. I have a table
I'm struggling to write a Windows Service that accesses a website, logs in using
I'm struggling to write Makefiles that properly build my unit tests. As an example,
Using Doctrine , I am trying to delete records in a single table based
I am trying to write a query that would select data from multiple lines
I am struggling to write/configure a ThrowsAdvice interceptor that I want to intercept all
I am struggling to write an AND conditional filter in log4net. Had it been
I'm struggling while trying to write generic methods to save and load data from
I'm struggling with some SQL/php syntax issues here. I'm trying to write two functions.

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.