我有一个包含不同报告日期的全球销售数据的表,如下所示:
+------------+------+------------+---------+
| Closed | Open | Plan | Station |
+------------+------+------------+---------+
| 2018-10-23 | NULL | NULL | A |
| 2018-10-22 | NULL | NULL | NULL |
| 2018-10-22 | NULL | NULL | B |
| 2018-10-22 | NULL | NULL | NULL |
| NULL | NULL | 2018-10-23 | C |
| NULL | NULL | 2018-10-22 | NULL |
| NULL | NULL | 2018-10-22 | NULL |
+------------+------+------------+---------+
CREATE TABLE Orders
(Closed DATE,
Open DATE,
Plan DATE,
Station Char);
insert into Orders values ("2018-10-23",NULL,NULL, "A");
insert into Orders values ("2018-10-22",NULL,NULL, NULL);
insert into Orders values ("2018-10-22",NULL,NULL, "B");
insert into Orders values ("2018-10-22",NULL,NULL, NULL);
insert into Orders values (NULL,NULL,"2018-10-23", "C");
insert into Orders values (NULL,NULL,"2018-10-22", NULL);
insert into Orders values (NULL,NULL,"2018-10-22", NULL);
我想用最后知道的值填充station列,以得到下面所需的结果。
+------------+------+------------+---------+
| Closed | Open | Plan | Station |
+------------+------+------------+---------+
| 2018-10-23 | NULL | NULL | A |
| 2018-10-22 | NULL | NULL | A |
| 2018-10-22 | NULL | NULL | B |
| 2018-10-22 | NULL | NULL | B |
| NULL | NULL | 2018-10-23 | C |
| NULL | NULL | 2018-10-22 | C |
| NULL | NULL | 2018-10-22 | C |
+------------+------+------------+---------+
1条答案
按热度按时间iq0todco1#
假设有一个主键列(假设
id
)在表中,可用于定义Station
. 请记住,数据是以无序方式存储的,如果不定义特定的顺序和/或主键,我们就无法真正定义“最后一个已知”的值。Coalesce()
函数将用于处理null
的值Station
在某一行。然后,我们可以使用相关子查询来确定“最后一个已知”的值。结果
db fiddle视图