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

The Archive Base Latest Questions

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

I am trying to write a SQL Server query but have had no luck

  • 0

I am trying to write a SQL Server query but have had no luck and was wondering if anyone may have any ideas on how to achieve my query.

What i’m trying to do:

I have a table with several columns naming the ones that i am dealing with TaskID, StatusCode, Timestamp. Now this table just holds tasks for one of our systems that run throughout the day and when something runs it gets a timestamp and the statuscode depending on the status for that task.

Sometimes what happens is the task table will be updated with a new timestamp but the statusCode will not have changed since the last update of the task so for two or more consecutive rows of a given task the statusCode can be the same. When i say consecutive rows i mean with regards to timestamp.

So example task 88 could have twenty rows at statusCode 2 after which the status code changes to something else.

Now what i am trying to do with no luck at the moment is to retrieve a list from this table of all the tasks and the statuscodes and the timestamps but in the case where i have more than one consecutive row for a task with the same statuscode i just want to take the first row with the lowest timestamp and ignore the rest of the row until the statuscode for that task changes.

To make it simpler in this case you can assume that i have a taskid which i am filtering on so i am just looking at a single task.

Does anyone have any ideas as to how i can do this or perhaps something that i coudl probably read to help me?

Thanks
Irfan.

  • 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-13T09:09:03+00:00Added an answer on May 13, 2026 at 9:09 am

    This are a couple ways of getting what you want:

    SELECT
         T1.task_id,
         T1.status_code,
         T1.status_timestamp
    FROM
         My_Table T1
    LEFT OUTER JOIN My_Table T2 ON
         T2.task_id = T1.task_id AND
         T2.status_timestamp < T1.status_timestamp
    LEFT OUTER JOIN My_Table T3 ON
         T3.task_id = T1.task_id AND
         T3.status_timestamp < T1.status_timestamp AND
         T3.status_timestamp > T2.status_timestamp
    WHERE
         T3.task_id IS NULL AND
         (T2.status_code IS NULL OR T2.status_code <> T1.status_code)
    ORDER BY
         T1.status_timestamp
    

    or

    SELECT
         T1.task_id,
         T1.status_code,
         T1.status_timestamp
    FROM
         My_Table T1
    LEFT OUTER JOIN My_Table T2 ON
         T2.task_id = T1.task_id AND
         T2.status_timestamp = (
              SELECT
                   MAX(status_timestamp)
              FROM
                   My_Table T3
              WHERE
                   T3.task_id = T1.task_id AND
                   T3.status_timestamp < T1.status_timestamp)
    WHERE
         (T2.status_code IS NULL OR T2.status_code <> T1.status_code)
    ORDER BY
         T1.status_timestamp
    

    Both methods rely on there being no exact matches of the status_timestamp values (two rows can’t have the same exact status_timestamp for a given task_id.)

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

Sidebar

Ask A Question

Stats

  • Questions 278k
  • Answers 278k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The best way to form a SQL query is not… May 13, 2026 at 3:14 pm
  • Editorial Team
    Editorial Team added an answer For o and O, here are a few functions I… May 13, 2026 at 3:14 pm
  • Editorial Team
    Editorial Team added an answer I think the problem is that when you are calling… May 13, 2026 at 3:14 pm

Related Questions

I am trying to write a query for SQL Server 2005 but I can't
I'm trying to write a stored procedure and depending on a certain column value,
I am implementing web services for a PHP application and am trying to understand
I have an existing SQL Server database whose structure I can't really change, although
Environment I'm working on a C++ application that uses SQL Native Client 9.0 to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.