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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:52:43+00:00 2026-05-23T07:52:43+00:00

I am having difficulty writing a query. I need to combine rows of time-consecutive

  • 0

I am having difficulty writing a query. I need to combine rows of time-consecutive states into a single state. For example, given the data:

start                    end                      state   seconds
2011-04-21 08:13:30.000  2011-04-21 08:18:00.000  STATE1  270
2011-04-21 08:18:00.000  2011-04-21 08:22:30.000  STATE1  270
2011-04-21 08:22:30.000  2011-04-21 08:26:26.000  STATE1  236
2011-04-21 08:26:26.000  2011-04-21 08:26:47.000  STATE2  21
2011-04-21 08:26:47.000  2011-04-21 08:27:30.000  STATE3  43
2011-04-21 08:27:30.000  2011-04-21 08:28:20.000  STATE1  50
2011-04-21 08:40:30.000  2011-04-21 08:41:00.000  STATE1  30

I need to combine rows only when row2.state = row1.state AND row2.start = row1.end and come up with an overall start and end time of the state. The result should be:

start                    end                      state   seconds
2011-04-21 08:13:30.000  2011-04-21 08:26:26.000  STATE1  776
2011-04-21 08:26:26.000  2011-04-21 08:26:47.000  STATE2  21
2011-04-21 08:26:47.000  2011-04-21 08:27:30.000  STATE3  43
2011-04-21 08:27:30.000  2011-04-21 08:28:20.000  STATE1  50
2011-04-21 08:40:30.000  2011-04-21 08:41:00.000  STATE1  30

Here is the example SQL:

CREATE TABLE Data (
    ID int IDENTITY(1,1) not null,
    Date DateTime not null,
    State nvarchar(40) not null,
    Seconds int not null,
    PRIMARY KEY(ID)
);
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:13:30.000', 'STATE1', 270)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:18:00.000', 'STATE1', 270)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:22:30.000', 'STATE1', 236)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:26:26.000', 'STATE2', 21)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:26:47.000', 'STATE3', 43)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:27:30.000', 'STATE1', 50)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:40:30.000', 'STATE1', 30)

SELECT Date as 'start', DATEADD(s,Seconds,Date) as 'end', State, Seconds FROM Data

Thanks in advance!

  • 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-23T07:52:43+00:00Added an answer on May 23, 2026 at 7:52 am

    Try this(>= SQL Server 2005):

    WITH qry AS
    (
        SELECT  a.*
                        ,ROW_NUMBER() OVER (ORDER BY [start]) rn
        FROM    (SELECT Date as 'start', DATEADD(s,Seconds,Date) as 'end', State, Seconds FROM Data) a
    )
    SELECT  DISTINCT MIN(a.start) OVER(PARTITION BY a.State, a.[end] - ISNULL(b.start, a.start)) ,
                    MAX(a.[end] ) OVER(PARTITION BY a.State, a.[end] - ISNULL(b.start, a.start)) ,
                    a.state
                    ,SUM(a.Seconds) OVER(PARTITION BY a.State, a.[end] - ISNULL(b.start, a.start)) 
    
      FROM qry a LEFT JOIN qry b
            ON a.rn + 1 = b.rn
       AND a.[end] = b.start
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having difficulty writing a Stored Procedure that will query a list of
I am having some difficulty writing a function that will search through a directory
I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that
I am writing a GAE application and am having some difficulty with the following
I'm having difficulty writing a Criteria to select all entities with empty child collections
I'm writing a microsoft word macro and having difficulty with vb.net. Please help.
I'm writing an MSc dissertation and I'm having difficulty getting the longnamesfirst option working
I'm writing a tokenizer that reads code from a file and am having difficulty
I'm having some difficulty with writing some jQuery code. What i'm trying to do
I'm writing a plugin and having difficulty getting it to instantiate for multiple elements.

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.