简体   繁体   English

替代TOP或LIMIT条款?

[英]Alternative to TOP or LIMIT clause?

I am looking for an alternative to the TOP or LIMIT clause in the query below. 我在下面的查询中寻找TOP或LIMIT子句的替代方法。 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. 数据集中涉及20个引擎,只有一个如下所示。 About 180K rows per month. 每月大约180K行。

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. 数据库运行在下面链接的OpenAccess SQL。 TOP can be used but non in a subquery, derived table or in conjunction with a union. 可以使用TOP但不在子查询,派生表中或与联合一起使用。 LIMIT is not supported as far as I can tell. 据我所知,不支持LIMIT。

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

Perhaps using MAX() will work: 也许使用MAX()会起作用:

(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
)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM