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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:52:47+00:00 2026-06-09T23:52:47+00:00

I need a query to find the difference between two dates with the status

  • 0

I need a query to find the difference between two dates with the status condition,

My table data as like this:

select  
    ROW_NUMBER() OVER (ORDER BY eventtime) as id,
    Eventcode, eventtime, status 
from cfw.dbo.DCTBLEVENTINFO 
where
   MeterID = 4722
   and EventTime >= '2011-10-21' and EventCode = 13 

Data:

id  Eventcode eventtime                status
1   13        2011-10-21 21:42:00.000   1
2   13        2011-10-21 22:02:24.107   0
3   13        2011-10-22 09:45:00.000   1
4   13        2011-10-22 10:05:24.107   0
5   13        2011-10-22 15:08:00.000   0
6   13        2011-10-22 16:33:00.000   1
7   13        2011-10-22 16:44:00.000   0
8   13        2011-10-22 16:53:24.107   0
9   13        2011-10-23 08:52:00.000   1
10  13        2011-10-23 09:12:24.107   0
11  13        2011-10-23 10:18:00.000   0
12  13        2011-10-23 10:35:00.000   1
13  13        2011-10-23 10:36:00.000   0
14  13        2011-10-23 10:55:24.107   0
15  13        2011-10-23 10:56:00.000   1
16  13        2011-10-23 11:01:00.000   0
17  13        2011-10-23 11:16:24.107   0
18  13        2011-10-23 18:28:00.000   1
19  13        2011-10-23 18:30:00.000   0

In this status column 1 refers the occurencetime, 0 refers the restorationtime. I have to take the difference between the first occurencetime and the next corresponding restoration time then take the next occurencetime

for ex

For the date of 2011-10-22 totally 5 entries are there I have to take the occurencetime as 2011-10-22 09:45:00.000 and restoration time as 2011-10-22 10:05:24.107 and make a difference. next entry have the restorationtime alone(status 0) i have to leave that entry and search for next 1 & 0 entry. then make the difference.

My final resultset as like this:

Eventcode   occurencetime            restorationtime              difference (sec)
13          2011-10-21 21:42:00.000  2011-10-21 22:02:24.107      1224
13          2011-10-22 09:45:00.000  2011-10-22 10:05:24.107      1224 
13          2011-10-22 16:33:00.000  2011-10-22 16:44:00.000       660

Help me to do that.

  • 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-09T23:52:48+00:00Added an answer on June 9, 2026 at 11:52 pm

    try this:

     ;with cte as (
     select ROW_NUMBER() OVER (ORDER BY eventtime) as id,
            Eventcode, eventtime, status 
    from cfw.dbo.DCTBLEVENTINFO 
    where MeterID = 4722
    and EventTime >= '2011-10-21' and EventCode = 13)
    select   c1.Eventcode,
             c1.eventtime as occurencetime,
             min(c2.eventtime) as restorationtime,
             datediff(ss,c1.eventtime,min(c2.eventtime)) as difference
    from     cte c1
    left outer join cte c2
    on       c1.eventtime < c2.eventtime           
    and      c1.Eventcode=c2.Eventcode
    where    c1.status=1
    and      c2.status=0      
    group by c1.Eventcode,c1.eventtime
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need query to get data between two days.I tried with this.But it gives
I have two fields. homescore and awayscore. I need to find the difference between
I need to write a query that will go through a table, find duplicate
Based on the documentation here: http://docs.composite.net/Data/AccessingDataWithCSharp/How-to-Query-Data-Using-LINQ#_How_to_Query I need to query data in a table
I need a query to perform the following: find the countryID where country =
I need to write a query where I need to find the ' character.
I need to query statistics about duplicate values in my database table. For example,
I need to query large amount of data from a database via C# and
I'm looking for a query to select rows from two different tables, keeping the
I want to build a query like SELECT username from users WHERE login_attempts >

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.