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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:17:55+00:00 2026-06-11T04:17:55+00:00

I have a set with data in a SQL Server database and I need

  • 0

I have a set with data in a SQL Server database and I need to calculate uptime of a machine. I’m using two variables to determine uptime or downtime. These two variables are machine_ON and failure(s). machine_ON is only one variable in the database, failure can be 64 different failures, but all indicates as fx_x.

The status information of these variables is stored in the database as follow:

timestamp                   failurebitNr    timestampOutOfAlarm
2012-01-17 10:38:58.000     f1_14           2012-01-17 10:39:05.000

Meaning: failure f1_14 was active from 2012-01-17 10:38:58.000 until 2012-01-17 10:39:05.000

Also the machine_ON state is saved in the same table on the same way, only the failurebitNr has a different value [t2_13].

So to determine the uptime, I need to get the timediff between timestamp and timestampOutOfAlarm where failurebutNr = 't2_13' minus any failure time.

So for example I have those rows in my database:

Click for image of table representation

This should give the following graphical representation:

Graphical representation of the dataser

Green is uptime, red is downtime.

I’m used to work with PHP and than use a while loop and file some array’s and do other scripting. But now I need to do this all in a SQL Server database in query ways…

So, how can I calculate the uptime (green) and downtime (red)?

UPDATE

I’ve tried to get the time in seconds that the machine is ON. I used this query:

<!-- language: lang-sql -->
DECLARE @startDate datetime
DECLARE @endDate datetime
DECLARE @projectNr int
DECLARE @MachineNr nvarchar(10) 

SET @startDate = '2012-01-01 00:00:00.000'
SET @endDate  = '2012-02-01 00:00:00.000'
SET @projectNr = '1234567'
SET @MachineNr = '2'

SELECT 
    DATEDIFF("SECOND",
        CASE WHEN timestamp < @startDate 
            THEN @startDate 
            ELSE timestamp 
        END, 
        CASE WHEN timestampOutOfAlarm > @endDate OR timestampOutOfAlarm IS NULL 
            THEN @endDate
            ELSE timestampOutOfAlarm 
        END) AS Uptime
FROM 
    [table]
WHERE
     timestamp < @endDate
     AND (timestampOutOfAlarm > @startDate OR timestampOutOfAlarm IS NULL)
     AND fileProjectNr = @projectNr
     AND fileProjectMachineNr = @MachineNr
     AND (failureBitNr = 't2_13' AND failureBitValue = '1')
  • 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-11T04:17:56+00:00Added an answer on June 11, 2026 at 4:17 am

    Problem solved:

    I did it in 2 stored procedures, or Query’s. One for getting the “ON” time and one for getting all the “DOWN” time within the “ON” time. Whith those two times I can calculate the uptime, downtime, maintenance time.

    Query for ON time:

    <!-- language: lang-sql -->
    SELECT  fctAlarmId,
            fileProjectNr, 
            fileProjectMachineNr, 
            failureBitNr,   
            timestamp, 
            CASE WHEN timestampOutOfAlarm > @endDate OR timestampOutOfAlarm IS NULL 
                THEN @endDate
                ELSE timestampOutOfAlarm
            END 
            AS timestampOutOfAlarm
    INTO    #tempTable1
    FROM    fctAlarmHistory
    WHERE   (timestampOutOfAlarm >= @startDate OR timestampOutOfAlarm = NULL) 
            AND timestamp < @endDate
            AND failureBitValue = 1 
            AND failureBitNr = 't2_13'
            AND fileProjectNr = @projectNr
            And fileProjectMachineNr = @ProjectMachineNr
    
    -- SUM the result of all ON times into OnTime in seconds
    SELECT 
        SUM(DATEDIFF("SECOND",
            CASE WHEN timestamp < @startDate 
                THEN @startDate 
                ELSE timestamp 
            END, 
            CASE WHEN timestampOutOfAlarm > @endDate
                THEN @endDate 
                ELSE timestampOutOfAlarm 
            END)) AS OnTime
    FROM
        #tempTable1
    

    Query for Downtime during ON time:

    <!-- language: lang-sql -->
    SELECT  fctAlarmId,
            fileProjectNr, 
            fileProjectMachineNr, 
            failureBitNr,   
            timestamp, 
            CASE WHEN timestampOutOfAlarm > @endDate OR timestampOutOfAlarm IS NULL 
                THEN @endDate
                ELSE timestampOutOfAlarm
            END 
            AS timestampOutOfAlarm
    INTO    #tempTable1
    FROM    fctAlarmHistory
    WHERE   (timestampOutOfAlarm >= @startDate OR timestampOutOfAlarm = NULL) 
            AND timestamp < @endDate
            AND failureBitValue = 1 
            AND failureBitNr = 't2_13'
            AND fileProjectNr = @projectNr
            And fileProjectMachineNr = @ProjectMachineNr
    
    SELECT  fctAlarmId,
            fileProjectNr, 
            fileProjectMachineNr, 
            failureBitNr,   
            timestamp, 
            CASE WHEN timestampOutOfAlarm > @endDate OR timestampOutOfAlarm IS NULL 
                THEN @endDate
                ELSE timestampOutOfAlarm
            END 
            AS timestampOutOfAlarm
    INTO    #tempTable2
    FROM    fctAlarmHistory
    WHERE   (timestamp BETWEEN @startDate AND @endDate) 
            AND failureBitValue = 1
            AND (failureBitNr LIKE'f%')
            AND fileProjectNr = @projectNr
            And fileProjectMachineNr = @ProjectMachineNr
    
    CREATE TABLE #tempTable3
    (
    ID int IDENTITY(1,1),
    timestamp datetime,
    timestampOutOfAlarm datetime
    )
    
    DECLARE failure_Cursor CURSOR FOR
    SELECT timestamp, timestampOutOfAlarm
    FROM #tempTable2
    ORDER BY timestamp ASC
    
    OPEN failure_Cursor
    
    -- Perform the first fetch.
    FETCH NEXT FROM failure_Cursor
    INTO @rij_timestamp, @rij_timestampOutOfAlarm
    
    INSERT INTO #tempTable3 (timestamp, timestampOutOfAlarm) VALUES(@rij_timestamp,@rij_timestampOutOfAlarm)
    
    -- Check @@FETCH_STATUS to see if there are any more rows to fetch.
    WHILE @@FETCH_STATUS = 0
        BEGIN
            PRINT @rij_timestamp
            IF @rij_timestamp <= (SELECT TOP 1 timestampOutOfAlarm FROM #tempTable3 ORDER BY timestamp DESC)
            BEGIN   
                IF @rij_timestampOutOfAlarm > (SELECT TOP 1 timestampOutOfAlarm FROM #tempTable3 ORDER BY timestamp DESC)
                BEGIN
                    UPDATE #tempTable3 SET timestampOutOfAlarm = @rij_timestampOutOfAlarm WHERE ID = (SELECT TOP 1 ID FROM #tempTable3 ORDER BY timestamp DESC)
    
                END
            END
            ELSE
                INSERT INTO #tempTable3 (timestamp, timestampOutOfAlarm) VALUES(@rij_timestamp,@rij_timestampOutOfAlarm)
    
            FETCH NEXT FROM failure_Cursor
            INTO @rij_timestamp, @rij_timestampOutOfAlarm
        END
    CLOSE failure_Cursor
    DEALLOCATE failure_Cursor
    
    -- Select the failure time.
    SELECT 
        SUM(DATEDIFF("SECOND",
            CASE WHEN tt3.timestamp < @startDate 
                THEN @startDate 
                ELSE tt3.timestamp 
            END, 
            CASE WHEN tt3.timestampOutOfAlarm > tt1.timestampOutOfAlarm 
                THEN tt1.timestampOutOfAlarm 
                ELSE tt3.timestampOutOfAlarm 
            END)) AS DownTime
    FROM
        #tempTable3 tt3
    INNER JOIN
        #tempTable1 tt1
    ON
        tt3.timestamp BETWEEN tt1.timestamp AND tt1.timestampOutOfAlarm
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008 R2 and I have a database which contains two
I have SQL Server 2005 set up and I backed up a database a
i want to get data from sql server database in asp.net using NHibernate. what
I have a SQL Server database that contains an xml column. I need to
I have a column in the database (SQL Server 2005) that has data with
I have the requirement to search several different tables in my SQL Server database.
I have an audit table in my SQL Server 2008 database which contains the
We have built a large database model (SQL Server) for our client and an
I am using SDAC components to query a SQL Server 2008 database. It has
We have SQL Server database setup. We are setting up a replication scenarios where

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.