简体   繁体   中英

SQL: FIlter rows by direction

I have a table with 2 column date (timestamp), status (boolean). I have a lot of value like:

| date                      | status    |
|-------------------------- |--------   |
| 2018-11-05T19:04:21.125Z  | true      |
| 2018-11-05T19:04:22.125Z  | true      |
| 2018-11-05T19:04:23.125Z  | true      |
....

I need to get a result like this:

| date_from                 | date_to                   | status    |
|-------------------------- |-------------------------- |--------   |
| 2018-11-05T19:04:21.125Z  | 2018-11-05T19:04:27.125Z  | true      |
| 2018-11-05T19:04:27.125Z  | 2018-11-05T19:04:47.125Z  | false     |
| 2018-11-05T19:04:47.125Z  | 2018-11-05T19:04:57.125Z  | true      |

So, I need to filter all "same" value and get in return only period of status true/false.

I create query like this:

SELECT max("current_date"), current_status, previous_status
FROM (SELECT date as "current_date",
             status as current_status,
             (lag(status, 1) OVER (ORDER BY msgtime))::boolean AS previous_status
      FROM "table" as table
      ) as raw_data
group by current_status, previous_status

but in response I get only no more than 4 value

This is a gaps-and-islands problem. A typical method uses the difference of row numbers:

select min(date), max(date), status
from (select t.*,
             row_number() over (order by date) as seqnum,
             row_number() over (partition by status order by date) as seqnum_s
      from t
     ) t
group by status, (seqnum - seqnum_s);

Yes you could use LAG but then you also need a running counter that increments every time the status changes:

WITH cte1 AS (
    SELECT date, status, CASE WHEN LAG(status) OVER (ORDER BY date) = status THEN 0 ELSE 1 END AS chg
    FROM yourdata
), cte2 AS (
    SELECT date, status, SUM(chg) OVER (ORDER BY date) AS grp
    FROM cte1
)
SELECT MIN(date) AS date_from, MAX(date) AS date_to, status
FROM cte2
GROUP BY grp, status
ORDER BY date_from

DB Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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