1

I am looking for an alternative to the TOP or LIMIT clause in the query below. I want the output to be when the engine status changes from its previous value. I am going to union another table on the beginning and end so I can have the first value for the month and the last value for the month as well, but this is not shown below, for simplicity.Basically, I want to the columns output in the report when the status changes from one row to the next.

SELECT A.pointidlong 
       , A.pointtime 
       , A.value 
FROM   enginestatus A 
WHERE  A.pointidlong = 'engine1' 
   AND A.pointtime > Now() - 30 
   AND A.value <> (SELECT TOP 1 B.value 
                   FROM   enginestatus B 
                   WHERE  B.pointidlong = A.pointidlong 
                      AND B.pointtime < A.pointtime 
                      AND B.pointtime > Now() - 30 
                   ORDER  BY B.pointtime DESC) 
ORDER  BY pointidlong 
         , pointtime 

The small data set is below. There are 20 engines involved in the data set only one shown below. About 180K rows per month.

engine,        PointTime,         status
'engine1', '2016-02-14 15:30:00', 'RUNNING'
'engine1', '2016-02-14 15:36:00', 'RUNNING'
'engine1', '2016-02-14 15:51:00', 'RUNNING'
'engine1', '2016-02-14 16:06:00', 'STOPPED'
'engine1', '2016-02-14 16:20:00', 'RUNNING'
'engine1', '2016-02-14 16:35:00', 'RUNNING'
'engine1', '2016-02-14 16:51:00', 'RUNNING'
'engine1', '2016-02-14 17:05:00', 'STOPPED'
'engine1', '2016-02-14 17:20:00', 'RUNNING'
'engine1', '2016-02-14 17:35:00', 'STOPPED'
'engine1', '2016-02-14 17:50:00', 'RUNNING'
'engine1', '2016-02-14 18:05:00', 'RUNNING'
'engine1', '2016-02-14 18:19:00', 'STOPPED'
'engine1', '2016-02-14 18:36:00', 'RUNNING'
'engine1', '2016-02-14 18:51:00', 'RUNNING'

The database runs off of OpenAccess SQL which is linked below. TOP can be used but non in a subquery, derived table or in conjunction with a union. LIMIT is not supported as far as I can tell.

http://media.datadirect.com/download/docs/openaccess/sdk/openaccess_sql.pdf

tbst
  • 11
  • 2

1 Answers1

0

Perhaps using MAX() will work:

(SELECT B.value 
 FROM   enginestatus B 
 WHERE  B.pointidlong = A.pointidlong 
 AND B.pointtime < A.pointtime 
 AND B.pointtime > Now() - 30 
 AND B.pointtime = (SELECT MAX(pointtime)
                    FROM   enginestatus
)) 
Aaron Dietz
  • 10,137
  • 1
  • 14
  • 26