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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:50:25+00:00 2026-06-15T21:50:25+00:00

I have a question related to Sql Server 2005 ( Tsql) I have two

  • 0

I have a question related to Sql Server 2005 ( Tsql)

I have two columns
Date
Status

And the Data looks.. like

Date                    Status

2012-09-01 00:01:00.000 2

2012-09-01 04:17:00.000 4

2012-09-01 04:34:00.000 4

2012-09-01 04:35:00.000 4

2012-09-01 04:35:48.000 4

2012-09-01 04:35:51.000 1

2012-09-01 17:28:25.000 2

2012-09-01 23:58:00.000 4

2012-09-01 23:59:00.000 1

I need to calculate the time difference between the status … for example. for min time is when
status 2 start and max time when status= 1 (stop) between that date i want the number of minutes.
I have done through the cursors checking the status and recording the minumum time and maximum time in the variables

Do we have any easy of doing it using CTE.

MY QUERY TAKES LONG TIME TO FINISH … PLEASE HELP.

   DECLARE @pdunitid INT
        DECLARE @Date DATETIME
        DECLARE @pddatetime DATETIME
        DECLARE @pdstatus INT
        DECLARE @starttime DATETIME
        DECLARE @endTime DATETIME
        DECLARE @calc INT
        DECLARE @Totaltime INT
        DECLARE @START INT


        SET @pdunitid = 33568906
        SET @Date = GETDATE() - 102
        set @Totaltime = 0
        SET @calc = 0
        SET @START = 0


        DECLARE s CURSOR FAST_FORWARD FOR
        SELECT pddatetime,pdstatus FROM s1 WITH (NOLOCK)
        WHERE  pdunitid = @pdUnitid
        AND CONVERT(VARCHAR,pddatetime,112) = CONVERT(VARCHAR,@Date,112)
        ORDER BY pddatetime,pdstatus 

        OPEN s 

        FETCH NEXT FROM s INTO @pddatetime,@pdstatus
        WHILE @@FETCH_STATUS = 0 
            BEGIN
                -- status 2 is for start sometimes you don't get start so you will have to use the first date with status 4

                IF pdstatus IN (2,4)  AND @START = 0 
                    BEGIN 
                        SET @starttime = @pddatetime
                        SET @START = 1
                    END

                -- status 1 is for stop 

                IF (@pdstatus= 1) 
                    BEGIN 
                        SET @endTime = @pddatetime
                        SET @calc = 1   
                    END

                -- if you dont get the status 1 by '23:59:00' take the end time


            IF  convert(varchar,@pdgpsdatetime,108) = '23:59:00'
                    BEGIN 
                        SET @endTime = @pddatetime
                        SET @calc = 1   
            END

                -- Calculate the minutes.

                IF @calc = 1 
                    BEGIN 
                        SET @Totaltime = @Totaltime + DATEDIFF(mi, @starttime,@endTime)
                        SET @calc = 0   
                        SET @START = 0
                    END
        FETCH NEXT FROM s INTO @pddatetime,@pdstatus
            END
        CLOSE s
        DEALLOCATE s

        SELECT  @Totaltime
  • 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-15T21:50:27+00:00Added an answer on June 15, 2026 at 9:50 pm

    Try this, instead of #tbl put your table name:

    DROP TABLE #tbl
    CREATE TABLE #tbl([Date] DATETIME, [Status] INT)
    INSERT  #tbl
    VALUES  
    ('2012-09-01 00:01:00.000',  2),
    ('2012-09-01 04:17:00.000',  4),
    ('2012-09-01 04:34:00.000',  4),
    ('2012-09-01 04:35:00.000',  4),
    ('2012-09-01 04:35:48.000',  4),
    ('2012-09-01 04:35:51.000',  1),
    ('2012-09-01 17:28:25.000',  2),
    ('2012-09-01 23:58:00.000',  4),
    ('2012-09-01 23:59:00.000',  1),
    
    -- for 2012-09-05 there are no statuses 1 or 2
    --('2012-09-05 00:01:00.000',  2),
    ('2012-09-05 04:17:00.000',  4),
    ('2012-09-05 04:34:00.000',  4),
    ('2012-09-05 04:35:00.000',  4),
    ('2012-09-05 04:35:48.000',  4),
    ('2012-09-05 04:35:51.000',  4),
    ('2012-09-05 17:28:25.000',  4),
    ('2012-09-05 23:58:00.000',  4)
    --('2012-09-05 23:59:00.000',  1)
    
    ;WITH tbl AS 
    (
        SELECT  *,
                ROW_NUMBER() OVER (ORDER BY [Date]) id
        FROM    #tbl
    ), 
    b AS 
    (
        SELECT  MIN([Date]) MinDate,
                MAX([Date]) MaxDate,
                CAST([Date] AS DATE) dateWithoutTime
        FROM    tbl
        GROUP   BY CAST([Date] AS DATE)
    ),
    a AS
    (
        SELECT  *,
                ROW_NUMBER() OVER (ORDER BY [Date]) num
        FROM    tbl
        LEFT JOIN 
                b ON 
                b.MaxDate = [Date]
        OR      b.MinDate = [Date]
        WHERE   [Status] IN (2, 1)
        OR      [Date] = CASE WHEN NOT EXISTS (SELECT 1 FROM #tbl c WHERE c.Status = 2 AND CAST(c.[Date] AS DATE) = b.dateWithoutTime) THEN b.MinDate END
        OR      [Date] = CASE WHEN NOT EXISTS (SELECT 1 FROM #tbl c WHERE c.Status = 1 AND CAST(c.[Date] AS DATE) = b.dateWithoutTime) THEN b.MaxDate END
    )
    
    SELECT  
            tbl.Date,
            tbl.Status,
            CASE WHEN a2.num % 2 = 0 THEN DATEDIFF(MINUTE, a1.Date, a2.Date) END Diff
    FROM    tbl
    LEFT JOIN   
            a a2 ON tbl.id = a2.id
    LEFT JOIN   
            a a1 ON a2.num = a1.num + 1
    ORDER BY [Date]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question related with NULL values I am connecting to SQL Server
I have lots of article store in MS SQL server 2005 database in a
We use calculated columns in a few SQL Server 2005 tables which always return
I have implemented a secure SSL connection on the SQL Server 2005 server. I
We have set up a Windows 2008 server with MS SQL 2005 (default instance),
i have the following table data in ms sql server table name category and
A technology machine uses SQL Server Express to collect the data like temperature... The
I have a sql question that is closely related to this question - SQL
I am creating a new SQL Server 2008 database. I have two two tables
I have an asp.net website on a server and the db MS SQL 2005

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.