简体   繁体   English

使用SQL查询检测两个或多个连续行是否具有相同条目

[英]Detect if two or more consecutive rows have the same entry with a sql query

I have an "alert" table in my database, that stores my web apps status codes. 我的数据库中有一个“警报”表,用于存储我的Web应用程序状态代码。 These status codes are obtained from a task scheduler, written in python, that periodically queries the front and backend api, and stores the resulting status code returned in the response into their respective columns: frontend_status and backend_status . 这些状态代码是从用python编写的任务调度程序中获得的,该任务调度程序会定期查询前端和后端api,并将响应中返回的结果状态代码存储到各自的列中: frontend_statusbackend_status

I'm trying to do a simple (at least the idea sounds simple) sql query to check if two or more consecutive entries in one of these columns is anything other than 200 (meaning something could be wrong). 我正在尝试做一个简单的(至少听起来很简单)sql查询,以检查这些列之一中的两个或多个连续条目是否不是200(这可能是错误的)。 If it is, I want to use a boolean to take action. 如果是这样,我想使用布尔值采取措施。 I am not sure if I am missing something obvious but some help would be great. 我不确定是否遗漏了一些明显的东西,但是会有一些帮助。

I created a table in postgres, that looks something like this: 我在postgres中创建了一个表,看起来像这样:

-- Table: public.alert_001

-- DROP TABLE public.alert_001;

CREATE TABLE public.alert_001
(
    data_id integer NOT NULL DEFAULT nextval('alert_001_data_id_seq'::regclass),
    when_captured timestamp without time zone NOT NULL,
    frontend_status real,
    backend_status real,
    CONSTRAINT alert_001_pkey PRIMARY KEY (data_id)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.alert_001
    OWNER to postgres;

-- Index: alert_001_when_index

-- DROP INDEX public.alert_001_when_index;

CREATE INDEX alert_001_when_index
    ON public.alert_001 USING btree
    (when_captured)
    TABLESPACE pg_default;

I then managed to write a query that counts the number of entries that are a certain value in the last hour. 然后,我设法编写了一个查询,该查询计算了过去一个小时中某个值的条目数。 If the counted entries are more than 2 for example, it will return a boolean true. 例如,如果计数的条目大于2,则它将返回布尔值true。 However, although this works when two entries are picked up in the last hour, they are not necessarily consecutive entries. 但是,尽管这在最后一个小时拿到两个条目时有效,但它们不一定是连续的条目。 So my sql query looks something like: 所以我的SQL查询看起来像:

SELECT (SELECT COUNT(*) AS "Count" 
FROM alert_001 WHERE when_captured > NOW() - '1 hour'::INTERVAL AND backend_status != 200) > 2

Could anyone point me into the right direction to catch three or more consecutive entries that are not of status code 200? 谁能指出我正确的方向,以捕获三个或更多状态代码为200的连续条目?

Here is an example of the entries in the alert_001 table: 这是alert_001表中条目的示例:

when_captured,frontend_status, backend_status
'2018-02-02 14:55:19.63941','200','200'
'2018-02-02 14:54:19.636386','200','503'
'2018-02-02 14:53:19.636055','200','503'
'2018-02-02 14:52:19.631958','200','503'
'2018-02-02 14:51:19.62166','200','200'
'2018-02-02 14:50:19.621363','200','200'
'2018-02-02 14:49:19.612434','200','200'
'2018-02-02 14:48:19.611919','200','200'
'2018-02-02 14:47:19.610065','200','200'
'2018-02-02 14:46:19.607846','200','200'

And as you can see there are three consecutive entries with a 503 backend_status code, which I want to catch. 正如您所看到的,我要捕获三个带有503 backend_status代码的连续条目。 So, the expected output of the correct sql query could be True if more than three are found or False otherwise? 因此,如果找到三个以上,则正确的SQL查询的预期输出可能为True ,否则为False

You should use window functions for this: 您应该为此使用窗口函数:

SELECT EXISTS (
   SELECT 1 FROM
      (SELECT data_id,
              backend_status status,
              lag(backend_status) OVER w status_1,
              lag(backend_status, 2) OVER w status_2
         FROM alert_001
         WHERE when_captured > current_timestamp - INTERVAL '1 hour'
         WINDOW w AS (ORDER BY when_captured)
      ) last_three
   WHERE status <> 200
     AND status_1 <> 200
     AND status_2 <> 200
);

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

相关问题 SQL查询显示连续3行或更多行且人数超过100人的记录 - SQL query to display the records which have 3 or more consecutive rows and the amount of people more than 100 当两个或多个连续行具有相同的状态时,如何 select 一行 - How to select one row when two or more consecutive rows have same statuses SQL-查找相同ID的两个连续行 - SQL - find two consecutive rows for the same ID 比较 Big Query 中 A 列具有不同值且 B 列具有相同值的连续两行中的时间戳 - comparing timestamps in two consecutive rows which have different values for column A and the same value for column B in Big Query 如何过滤查询 Sql 以便在同一表的多个行具有两个相同值时仅显示一个值? - How to filter a Query Sql so that it shows only one value when more rows of the same table have two identical values? ORACLE SQL:减去两个或多个连续行的日期 - ORACLE SQL: Subtract dates of two or more consecutive rows 显示 3 个或更多连续行(Sql) - display 3 or more consecutive rows(Sql) 在sql中找到同一个表中两个连续行之间的时间差 - Find the time difference between two consecutive rows in the same table in sql 改进SQL查询以计算两个连续行之间的时间跨度 - Improve SQL query to calculate timespan between two consecutive rows SQL查询两个连续的行 - SQL Querying for two Consecutive Rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM