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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:42:16+00:00 2026-06-12T01:42:16+00:00

I often verify logs in SQLServer and my query usually looks like this (where

  • 0

I often verify logs in SQLServer and my query usually looks like this (where Type = 0 means error):

SELECT * 
FROM Logs 
WHERE Type = 0 
ORDER BY Timestamp

But most of the time, I’m not only interested by the error itself but also at what happened immediately before the error.

Is it possible, with SQLServer, to query n lines above/under (relative to the primary key) each line matching the WHERE clause of the query?

Ex. With my query I would only get lines 125 & 130. I would like [123, 124, 125] and [128, 129, 130].

PrimaryKey  Timestamp             Type  Description
123         2012-09-17 03:41:46.240 1   Working.
124         2012-09-17 03:42:46.240 1   Database backup.
125         2012-09-17 03:43:46.240 0   Access violation.
126         2012-09-17 03:44:46.240 1   Working.
127         2012-09-17 03:45:46.240 1   Working.
128         2012-09-17 03:46:46.240 1   Working.
129         2012-09-17 03:47:46.240 1   Backup.
130         2012-09-17 03:48:46.240 0   Corrupted.
131         2012-09-17 03:49:46.240 1   Working.

Thank you.

  • 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-12T01:42:17+00:00Added an answer on June 12, 2026 at 1:42 am

    I would do it as follows:

    SELECT 
        L2.* 
    FROM 
        Logs L1
        JOIN Logs L2
        ON 
            L1.PrimaryKey = L2.PrimaryKey OR 
            L1.PrimaryKey = L2.PrimaryKey - 1 OR
            L1.PrimaryKey = L2.PrimaryKey + 1
    WHERE
        L1.Type = 0
    

    The results would be as follows:

    PrimaryKey  TS                      Type        Description
    ----------- ----------------------- ----------- -----------------
    124         2012-09-17 03:42:46.240 1           Database backup.
    125         2012-09-17 03:43:46.240 0           Access violation.
    126         2012-09-17 03:44:46.240 1           Working.
    129         2012-09-17 03:47:46.240 1           Backup.
    130         2012-09-17 03:48:46.240 0           Corrupted.
    131         2012-09-17 03:49:46.240 1           Working.
    

    You can modify the join conditions using the relational operators to retrieve n lines above and below the matching line.

    If the PrimaryKey column is not guaranteed to be sequential, then assuming that the records are always ordered by Timestamp in ASC order, the following query will fetch the before and after records to a selected one:

    WITH LogsTable (PrimaryKey, TS, Type, Description, Rank) AS
    (
        SELECT 
            PrimaryKey, 
            TS, 
            Type, 
            Description, 
            ROW_NUMBER() OVER (ORDER BY TS ASC) as 'Rank' 
        FROM 
            Logs
    )
    SELECT 
        L2.* 
    FROM 
        LogsTable L1
        JOIN LogsTable L2
        ON 
            L1.Rank = L2.Rank OR 
            L1.Rank = L2.Rank - 1 OR
            L1.Rank = L2.Rank + 1
    WHERE
        L1.Type = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Often I use cursors in this way: for rec in (select * from MY_TABLE
Often times I write some SQL like this: string sql = @ -- Multi-line
Often when writing PHP I'll have it output some HTML like this - echo
Often, I would like to build up complex regexps from simpler ones. The only
Often when I am writing PHP I construct conditionals like this: if(1 === $var1
Often I do mvn install , the error: [INFO] Tests Completed in 8212ms ...
Often I would move files locally say from one folder to another. When committing
Often I see layouts on the iPhone like the one I attached. How is
Often I get an NSInternalInconsistencyException with different error messages, can any buddy give the
Often, when a script has a redirect loop we get an error in Google

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.