查找最大孤岛大小SQL

时间:2018-08-29 12:23:06

标签: sql amazon-redshift

当前sql代码会将所有最大孤岛大小总和为一。我试图仅找出 连续小时小时的最大岛屿大小,而忽略日期时间内的分钟和秒钟。

 select deviceid, grp -- min(readdate) as mn, max(readdate) as mx
       from (  select deviceid, readdate, sum(case when prev_rd >= readdate - interval '1 hour' then 1 else 0 end) over (partition by deviceid order by readdate rows between unbounded preceding and current row) as grp
               from (
                    select deviceid, readdate, lag(readdate) over (partition by deviceid order by readdate) as prev_rd
                    from metersWithExcess
                    order by readdate)
            ) tt
            group by deviceid, readdate, grp
           -- having mx > mn + 1 * interval '1 hour'

是否有一种方法可以返回具有最大大小的设备ID?我试图找到最长的连续小时读数

数据如下:

    deviceid    readdate
91428   2018-08-27 18:04:42
91428   2018-08-27 17:04:42
91428   2018-08-27 16:04:42
91428   2018-08-27 12:04:42
91428   2018-08-27 11:04:42
91428   2018-08-26 20:04:42
91428   2018-08-26 17:04:40
91428   2018-08-26 16:04:40
91428   2018-08-26 15:04:40
91428   2018-08-26 12:04:40
91489   2018-08-26 06:00:49
91493   2018-08-27 06:58:44
91511   2018-08-27 12:57:01
91511   2018-08-27 10:57:01
91511   2018-08-27 09:57:01
91511   2018-08-26 23:57:01
91511   2018-08-26 22:57:01
91511   2018-08-26 20:57:01
91511   2018-08-26 13:56:59
91511   2018-08-26 12:56:59
91511   2018-08-26 10:56:59
91547   2018-08-26 16:59:07
91547   2018-08-26 15:59:07
91547   2018-08-26 14:59:07
91547   2018-08-26 11:59:07
91547   2018-08-26 04:59:07
91582   2018-08-27 12:01:45
91582   2018-08-26 19:01:45

在此示例中:91428的最大大小为3,因为ID 91428的最大大小为18:04:42 / 17:04:42 / 16:04:42

1 个答案:

答案 0 :(得分:0)

您提出问题的方式有点令人困惑,“孤岛”和“最大规模”术语必须特定于您的行业,但是这种蛮力方法会有所帮助吗?格式为:

deviceid,第一个读数,与下一个读数的距离,第二个读数,与下一个读数的距离等

我注释掉了我的标准“ WHERE gap1 = '01:00:00'AND gap2 = '01:00:00'”,但这将找到您连续的每小时读数。

WITH DEVICEID_TEST_DATA   AS


(     SELECT  CAST(91428  AS  bigint) AS deviceid   ,  CAST('2018-08-27 18:04:42' AS TIMESTAMP)  AS readdate UNION
      SELECT  91428     ,   '2018-08-27 17:04:42'         UNION
      SELECT  91428     ,   '2018-08-27 16:04:42'         UNION
      SELECT  91428     ,   '2018-08-27 12:04:42'         UNION
      SELECT  91428     ,   '2018-08-27 11:04:42'         UNION
      SELECT  91428     ,   '2018-08-26 20:04:42'         UNION
      SELECT  91428     ,   '2018-08-26 17:04:40'         UNION
      SELECT  91428     ,   '2018-08-26 16:04:40'         UNION
      SELECT  91428     ,   '2018-08-26 15:04:40'         UNION
      SELECT  91428     ,   '2018-08-26 12:04:40'         UNION
      SELECT  91489     ,   '2018-08-26 06:00:49'         UNION
      SELECT  91493     ,   '2018-08-27 06:58:44'         UNION
      SELECT  91511     ,   '2018-08-27 12:57:01'         UNION
      SELECT  91511     ,   '2018-08-27 10:57:01'         UNION
      SELECT  91511     ,   '2018-08-27 09:57:01'         UNION
      SELECT  91511     ,   '2018-08-26 23:57:01'         UNION
      SELECT  91511     ,   '2018-08-26 22:57:01'         UNION
      SELECT  91511     ,   '2018-08-26 20:57:01'         UNION
      SELECT  91511     ,   '2018-08-26 13:56:59'         UNION
      SELECT  91511     ,   '2018-08-26 12:56:59'         UNION
      SELECT  91511     ,   '2018-08-26 10:56:59'         UNION
      SELECT  91547     ,   '2018-08-26 16:59:07'         UNION
      SELECT  91547     ,   '2018-08-26 15:59:07'         UNION
      SELECT  91547     ,   '2018-08-26 14:59:07'         UNION
      SELECT  91547     ,   '2018-08-26 11:59:07'         UNION
      SELECT  91547     ,   '2018-08-26 04:59:07'         UNION
      SELECT  91582     ,   '2018-08-27 12:01:45'         UNION
      SELECT  91582     ,   '2018-08-26 19:01:45'


)


SELECT  *     FROM   (

SELECT  deviceid ,  readdate  ,   
readdate - (LAG(readdate, 1)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC) ) AS gap1  ,
LAG(readdate, 1)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC) AS  readdate_1down  ,
(LAG(readdate, 1)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC)) - (LAG(readdate, 2)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC))    AS   gap2  ,
LAG(readdate, 2)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC) AS  readdate_2down ,
(LAG(readdate, 2)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC)) - (LAG(readdate, 3)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC))    AS   gap3  ,
LAG(readdate, 3)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC) AS  readdate_3down  ,
(LAG(readdate, 3)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC)) - (LAG(readdate, 4)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC))    AS   gap4  ,
LAG(readdate, 4)  OVER (PARTITION BY deviceid  ORDER BY  readdate ASC) AS  readdate_4down
FROM  DEVICEID_TEST_DATA
-------WHERE   deviceid  =  91428

)
--------WHERE   gap1 = '01:00:00'  AND   gap2  =  '01:00:00'
ORDER BY  deviceid  ,  readdate DESC