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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:27:07+00:00 2026-05-22T15:27:07+00:00

I have this big table called StateChanges (1.9 million rows) from a third party

  • 0

I have this big table called StateChanges (1.9 million rows) from a third party application that contains this data:

[ComponentID] : integer : Id of what changed
[NewStatus] : integer : How it changed (0: OK, 1: Error, 2:Warning)
[ConsoleTimeStamp] : timestamp, index : When it changed.

I need to display the various changes in state for a bunch of components between two dates. This is fairly trivial. However, I display my data this way:

[OldStatus] --> [New Status] | [ConsoleTimeStamp]

The problem I have is that, in order to get the ‘initial’ status of any given component, I need to get the rows that immediately before the initial date for all the components. There isn’t any range we can be sure of, there could have been no changes between 2003 and now.

For a single component, i could get this query to work fast enough:

SELECT TOP 1 [NewStatus], [ConsoleTimeStamp] FROM [StateChanges] 
WHERE [ComponentID] = ? AND [ConsoleTimeStamp] < ?
ORDER BY [ConsoleTimeStamp] DESC

Now, is there a way to get all the “previous states” for all of my components (the ids are in an array) in an effective manner? I’ve tried:

SELECT ComponentId, NewStatus, MAX(ConsoleTimeStamp) As LastDate FROM StateChanges
WHERE ComponentId IN ({0}) AND ConsoleTimeStamp <= ?
GROUP BY ComponentId,NewStatus
ORDER BY ComponentId ASC, LastDate ASC

This solutions just happens to be real slow (and give extra results i don’t need) compared to the other query that fetches the entire set of changes between the two dates.

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-05-22T15:27:08+00:00Added an answer on May 22, 2026 at 3:27 pm

    I’m not entirely sure I totally understand your question – but one approach would be to use a CTE (Common Table Expression) if you’re on SQL Server 2005 and newer (you aren’t specific enough in that regard).

    With this CTE, you can partition your data by some criteria – i.e. your ComponentId – and have SQL Server number all your rows starting at 1 for each of those partitions, ordered by some other criteria – i.e. probably ConsoleTimeStamp.

    So try something like this:

    ;WITH PartitionedComponents AS
    (
       SELECT 
           ComponentId, NewStatus, ConsoleTimeStamp,
           ROW_NUMBER() OVER(PARTITION BY ComponentId ORDER BY ConsoleTimeStamp DESC) AS 'RowNum'
       FROM 
           dbo.StateChanges
       WHERE
           ComponentId IN (.....) 
           AND ConsoleTimeStamp <= (threshold)
    )
    SELECT 
       ComponentId, NewStatus, ConsoleTimeStamp, RowNum
    FROM 
       PartitionedComponents
    WHERE
       RowNum <= 2
    

    Here, I am selecting only the last two entries for each “partition” (i.e. for each ComponentId) – ordered in a descending fashion by the ConsoleTimeStamp.

    Does that approach what you’re looking for??

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

Sidebar

Related Questions

I have this big data-entry sort of page, a table kind of layout using
I have a table that looks something like this: word big expensive smart fast
Here's the deal. I have a big class hierarchy and I have this one
Outline OK, I have Google'd this and already expecting a big fat NO!! But
I have multiple setTimeout functions like this: function bigtomedium(visiblespan) { visiblespan.removeClass('big').addClass('medium'); setTimeout(function(){ mediumtosmall(visiblespan);},150); };
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need
I have a table called Basic , start_time is one field with type :VARCHAR(5),
Here is the scenario 1. I have a table called items, inside the table
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM

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.