如何使用 LAG 从 MYSQL table 中获取最后一个非空值

How to get the last non null value from the MYSQL table with the LAG

我正在绑定一个 LAG 函数来从 table 中检索最后一个非空值,以替换一行中的空值。 我使用的代码:

SELECT ticket_id, business_area, priority ,client_name, 
        closed_date, closed_date_id,Next_Create_date, 
        case 
            when Next_Create_date is null and Ticket_ID = 0 
            then LAG(Next_Create_date) 
                over (partition by ticket_id, business_area, 
                            priority, client_name, 
                            closed_date, Closed_Date_ID 
                        order by Next_Create_date desc 
                     ) 
            else Next_Create_date 
        end  Next_Create_date2
from ams_auto.No_Issue_Time_NM a
order by closed_date desc ;

结果如下:

每个黄色 Next_Created_Date2 字段都应该有最后一个 NON null 值。在这种情况下,它将是“2021-07-16 00:44:17”,但由于某种原因它不起作用。

关于如何实现这一点有什么想法吗?

示例数据:

ticket_id business_area priority client_name closed_date Next_Create_date
61622 RMS Severity 3 TB 7/22/2021 22:49 8/29/2021 0:46
0 RMS Severity 1 TB 7/19/2021 2:00 8/29/2021 0:46
0 RMS Severity 2 TB 7/19/2021 2:00 8/29/2021 0:46
0 RMS Severity 1 TB 7/12/2021 2:00 8/29/2021 0:46
0 RMS Severity 2 TB 7/12/2021 2:00 8/29/2021 0:46
0 RMS Severity 3 TB 7/12/2021 2:00 8/29/2021 0:46
0 RMS Severity 1 TB 7/5/2021 2:00 8/29/2021 0:46
0 RMS Severity 2 TB 7/5/2021 2:00 8/29/2021 0:46
0 RMS Severity 3 TB 7/5/2021 2:00 8/29/2021 0:46
0 RMS Severity 1 TB 6/28/2021 2:00 null
0 RMS Severity 2 TB 6/28/2021 2:00 null
0 RMS Severity 3 TB 6/28/2021 2:00 null
0 RMS Severity 1 TB 6/21/2021 2:00 null
0 RMS Severity 2 TB 6/21/2021 2:00 null
0 RMS Severity 3 TB 6/21/2021 2:00 null
0 RMS Severity 1 TB 6/14/2021 2:00 null
0 RMS Severity 2 TB 6/14/2021 2:00 null
0 RMS Severity 3 TB 6/14/2021 2:00 null
0 RMS Severity 1 TB 6/7/2021 2:00 null
0 RMS Severity 2 TB 6/7/2021 2:00 null
0 RMS Severity 3 TB 6/7/2021 2:00 null
0 RMS Severity 1 TB 5/31/2021 2:00 7/16/2021 0:44
0 RMS Severity 2 TB 5/31/2021 2:00 7/16/2021 0:44
0 RMS Severity 3 TB 5/31/2021 2:00 7/16/2021 0:44
57765 RMS Severity 3 TB 5/28/2021 2:35 7/16/2021 0:44
57615 RMS Severity 3 TB 5/27/2021 2:24 5/28/2021 0:56

这是期望的结果:

ticket_id business_area priority client_name closed_date Next_Create_date
61622 RMS Severity 3 TB 7/22/2021 22:49 8/29/2021 0:46
0 RMS Severity 1 TB 7/19/2021 2:00 8/29/2021 0:46
0 RMS Severity 2 TB 7/19/2021 2:00 8/29/2021 0:46
0 RMS Severity 1 TB 7/12/2021 2:00 8/29/2021 0:46
0 RMS Severity 2 TB 7/12/2021 2:00 8/29/2021 0:46
0 RMS Severity 3 TB 7/12/2021 2:00 8/29/2021 0:46
0 RMS Severity 1 TB 7/5/2021 2:00 8/29/2021 0:46
0 RMS Severity 2 TB 7/5/2021 2:00 8/29/2021 0:46
0 RMS Severity 3 TB 7/5/2021 2:00 8/29/2021 0:46
0 RMS Severity 1 TB 6/28/2021 2:00 7/16/2021 0:44
0 RMS Severity 2 TB 6/28/2021 2:00 7/16/2021 0:44
0 RMS Severity 3 TB 6/28/2021 2:00 7/16/2021 0:44
0 RMS Severity 1 TB 6/21/2021 2:00 7/16/2021 0:44
0 RMS Severity 2 TB 6/21/2021 2:00 7/16/2021 0:44
0 RMS Severity 3 TB 6/21/2021 2:00 7/16/2021 0:44
0 RMS Severity 1 TB 6/14/2021 2:00 7/16/2021 0:44
0 RMS Severity 2 TB 6/14/2021 2:00 7/16/2021 0:44
0 RMS Severity 3 TB 6/14/2021 2:00 7/16/2021 0:44
0 RMS Severity 1 TB 6/7/2021 2:00 7/16/2021 0:44
0 RMS Severity 2 TB 6/7/2021 2:00 7/16/2021 0:44
0 RMS Severity 3 TB 6/7/2021 2:00 7/16/2021 0:44
0 RMS Severity 1 TB 5/31/2021 2:00 7/16/2021 0:44
0 RMS Severity 2 TB 5/31/2021 2:00 7/16/2021 0:44
0 RMS Severity 3 TB 5/31/2021 2:00 7/16/2021 0:44
57765 RMS Severity 3 TB 5/28/2021 2:35 7/16/2021 0:44
57615 RMS Severity 3 TB 5/27/2021 2:24 5/28/2021 0:56

亲切的问候, 罗莎

我没有看到任何可以玩的测试数据,所以这里有一个简单的例子

with qq(q) as (
  select 1 union all
  select 2 union all
  select null union all
  select 3
)

select q, 
       lag(q, 1, 'nothing to show') over(order by q desc)
  from qq;

您可以对数据进行排序并在最后移动空值 - 只需将其降序排序(不要问我为什么)。

这是结果

q lag
1 nothing to show
2 1
3 2
3

这是 dbfiddle 可以玩的

更新。对于上传的测试用例,不需要使用 LAG,但您可能会发现这里的子查询很有用

select td.ticket_id,
       td.business_area,
       td.priority,
       td.client_name,
       td.closed_date,
       ifnull(td.next_create_date, (select min(next_create_date) 
                                      from test_data td_1
                                     where td_1.ticket_id = td.ticket_id
                                       and td_1.business_area = td.business_area
                                       and td_1.priority = td.priority
                                       and td_1.client_name = td.client_name
                                       and td_1.next_create_date > td.closed_date) 
             ) next_creation_date_fixed
  from test_data td
 order by closed_date desc;

需要提及的一件事是子查询通常会导致性能下降,因此如果您使用的是 mySQL >= 8.0.14,您可能会发现横向连接很有用。 详情见here

dbfiddle