具有案例/排名功能的LAG功能

时间:2019-04-16 20:43:43

标签: sql amazon-redshift

我在单个表的lbb_diag_value列中有两条消息('usb_port_reset','onewire_cable')。

我正在使用的两列是: created_at(带时间戳),lbb_diag_value

问题:我要为所有连续的usb_port_reset消息关联one_wire_cable,直到在表中找到另一个one_wire_cable。

例如:

         Created_at    lbb_diag_value
         1 PM          onewire_cable
         1:15 PM       usb_port_reset
         3:00 Pm       usb_port_reset
         12Pm          onewire_cable
         Some Time     usb_port_reset

当前解决方案:我正在使用lag函数并左联接,如果没有连续的usb_port_reset消息,此方法就可以正常工作。

下面是我的代码:

  WITH CTE
AS (
    SELECT e2.identifier
        ,e2.created_at
        ,e2.model
        ,e2.sw_pkg_version
        ,e2.type
        ,e2.lbb_diag_value
        ,e2.lbb_diag_type
    FROM (
        SELECT e1.identifier
            ,e1.created_at
            ,e1.model
            ,e1.sw_pkg_version
            ,e1.lbb_diag_type
            ,e1.lbb_diag_value
            ,e1.type
        FROM eld_messages e1
        WHERE e1.type = 'lbb_diag'
            AND e1.lbb_diag_type = 'usb_port_reset'
            AND { % condition created_filter % } e1.created_at { % endcondition % }
        ) e2
    )
    ,onewire
AS (
    SELECT e2.identifier
        ,e2.lbb_diag_value
        ,e2.created_at
        ,e2.type
        ,e2.lbb_diag_type
        ,e2.prev_lbb_diag_type
        ,e2.prev_created_at
        ,e2.prev_lbb_diag_value
        ,e2.model
        ,e2.sw_pkg_version
        ,e2.seqnum
    FROM (
        SELECT e1.identifier
            ,e1.created_at
            ,e1.lbb_diag_value
            ,e1.type
            ,e1.lbb_diag_type
            ,e1.event_id
            ,e1.model
            ,e1.sw_pkg_version
            ,LAG(e1.lbb_diag_type) OVER (
                PARTITION BY e1.identifier ORDER BY e1.created_at
                    ,e1.event_id DESC
                ) AS prev_lbb_diag_type
            ,LAG(e1.created_at) OVER (
                PARTITION BY e1.identifier ORDER BY e1.created_at
                    ,e1.event_id DESC
                ) AS prev_created_at
            ,LAG(e1.lbb_diag_value) OVER (
                PARTITION BY e1.identifier ORDER BY e1.created_at
                    ,e1.event_id
                ) AS prev_lbb_diag_value
            ,row_number() OVER (
                PARTITION BY e1.lbb_diag_type ORDER BY e1.created_at
                    ,e1.event_id DESC
                ) seqnum
        FROM eld_messages e1
        WHERE e1.type = 'lbb_diag'
            AND e1.lbb_diag_type IN (
                'onewire_cable'
                ,'usb_port_reset'
                )
            AND { % condition created_filter % } e1.created_at { % endcondition % }
        ORDER BY e1.identifier
            ,e1.created_at
        ) e2
    WHERE (
            e2.lbb_diag_type = 'usb_port_reset'
            AND e2.prev_lbb_diag_type = 'onewire_cable'
            )
        OR (
            CASE 
                WHEN e2.lbb_diag_type = 'usb_port_reset'
                    AND e2.prev_lbb_diag_type = 'usb_port_reset'
                    THEN e2.seqnum = 1
                END
            )
    )
SELECT cte.identifier
    ,cte.created_at
    ,cte.model
    ,cte.sw_pkg_version
    ,cte.type
    ,cte.lbb_diag_type
    ,cte.lbb_diag_value
    ,onewire.prev_lbb_diag_value AS onewire_lbb_diag_value
    ,onewire.prev_created_at AS onewire_created_at
FROM cte
LEFT JOIN onewire ON cte.identifier = onewire.identifier
    AND cte.created_at = onewire.created_at; 

1 个答案:

答案 0 :(得分:0)

将带有延迟的子选择嵌套起来并过滤掉lbb_diag_value不变的记录是否可行?我并没有尝试了解该庞大查询的整个上下文以及是否需要其他记录,但是滞后可能是检测似乎需要的边界的好方法。

FROM (
    SELECT * FROM (
        SELECT e1.identifier ,e1.created_at ,e1.lbb_diag_value ,e1.type ,e1.lbb_diag_type ,e1.event_id
            ,e1.model ,e1.sw_pkg_version
            ,LAG(e1.lbb_diag_type) OVER (PARTITION BY e1.identifier ORDER BY e1.created_at ,e1.event_id DESC) AS prev_lbb_diag_type
            ,LAG(e1.created_at) OVER ( PARTITION BY e1.identifier ORDER BY e1.created_at ,e1.event_id DESC) AS prev_created_at
            ,LAG(e1.lbb_diag_value) OVER ( PARTITION BY e1.identifier ORDER BY e1.created_at ,e1.event_id) AS prev_lbb_diag_value
            ,row_number() OVER (PARTITION BY e1.lbb_diag_type ORDER BY e1.created_at ,e1.event_id DESC) seqnum
        FROM eld_messages e1
        WHERE e1.type = 'lbb_diag' AND e1.lbb_diag_type IN ('onewire_cable' ,'usb_port_reset')
            AND { % condition created_filter % } e1.created_at { % endcondition % }
        ORDER BY e1.identifier, e1.created_at
    ) t
    WHERE lbb_diag_value != prev_lbb_diag_value
) e2

如果您需要所有记录,则可以通过连接将其分为两个子选择(将您留下的选择与检测过渡的选择结合在一起),其中过渡(lbb_diag_value!= prev_lbb_diag_value)最终作为新的列,指示从一个值翻转到另一个值的记录。

然后识别“ onewire_cable”电缆将类似于:

select * from (
...join...
) j where j.flip = 1 and j.lbb_diag_value = 'onewire_cable'