Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Problem solved:</p> <p>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.</p> <p>Query for ON time:</p> <pre><code>&lt;!-- language: lang-sql --&gt; SELECT fctAlarmId, fileProjectNr, fileProjectMachineNr, failureBitNr, timestamp, CASE WHEN timestampOutOfAlarm &gt; @endDate OR timestampOutOfAlarm IS NULL THEN @endDate ELSE timestampOutOfAlarm END AS timestampOutOfAlarm INTO #tempTable1 FROM fctAlarmHistory WHERE (timestampOutOfAlarm &gt;= @startDate OR timestampOutOfAlarm = NULL) AND timestamp &lt; @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 &lt; @startDate THEN @startDate ELSE timestamp END, CASE WHEN timestampOutOfAlarm &gt; @endDate THEN @endDate ELSE timestampOutOfAlarm END)) AS OnTime FROM #tempTable1 </code></pre> <p>Query for Downtime during ON time:</p> <pre><code>&lt;!-- language: lang-sql --&gt; SELECT fctAlarmId, fileProjectNr, fileProjectMachineNr, failureBitNr, timestamp, CASE WHEN timestampOutOfAlarm &gt; @endDate OR timestampOutOfAlarm IS NULL THEN @endDate ELSE timestampOutOfAlarm END AS timestampOutOfAlarm INTO #tempTable1 FROM fctAlarmHistory WHERE (timestampOutOfAlarm &gt;= @startDate OR timestampOutOfAlarm = NULL) AND timestamp &lt; @endDate AND failureBitValue = 1 AND failureBitNr = 't2_13' AND fileProjectNr = @projectNr And fileProjectMachineNr = @ProjectMachineNr SELECT fctAlarmId, fileProjectNr, fileProjectMachineNr, failureBitNr, timestamp, CASE WHEN timestampOutOfAlarm &gt; @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 &lt;= (SELECT TOP 1 timestampOutOfAlarm FROM #tempTable3 ORDER BY timestamp DESC) BEGIN IF @rij_timestampOutOfAlarm &gt; (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 &lt; @startDate THEN @startDate ELSE tt3.timestamp END, CASE WHEN tt3.timestampOutOfAlarm &gt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload