0

我有一个大约 20 列和超过 5000 行的表。对于每一行,我试图用字符串替换第一个空值"end"

我有的:

BEGIN TRANSACTION;

/* Create a table called dataframe */
CREATE TABLE dataframe(id integer primary key, col1 text, col2 text, col3 text, col4 text, col5 text);

/* Create few records in this table */
INSERT INTO dataframe VALUES(1,'a',null,null,null,null);
INSERT INTO dataframe VALUES(2,'a','b',null,null,null);
INSERT INTO dataframe VALUES(3,'a','c','d','e', null);
INSERT INTO dataframe VALUES(4,'a','c','d',null, null);
INSERT INTO dataframe VALUES(5,'a','c',null, null, null);
COMMIT;

/* Display all the records from the table */
SELECT * FROM dataframe;

我需要的:

BEGIN TRANSACTION;

/* Create a table called dataframe */
CREATE TABLE dataframe(id integer primary key, col1 text, col2 text, col3 text, col4 text, col5 text);

/* Create few records in this table */
INSERT INTO dataframe VALUES(1,'a','end',null,null,null);
INSERT INTO dataframe VALUES(2,'a','b','end',null,null);
INSERT INTO dataframe VALUES(3,'a','c','d','e', 'end');
INSERT INTO dataframe VALUES(4,'a','c','d','end', null);
INSERT INTO dataframe VALUES(5,'a','c','end', null, null);
COMMIT;

/* Display all the records from the table */
SELECT * FROM dataframe;

我可以FIRST_VALUE()在行级别上使用吗?

4

1 回答 1

4

多个case表达式可能是最简单的方法:

select df.id,
       (case when df.col1 is null
             then 'end' else df.col1
        end) as col1,
       (case when df.col1 is not null and df.col2 is null
             then 'end' else df.col2
        end) as col2,
       (case when df.col1 is not null and df.col2 is not null and df.col3 is null
             then 'end' else df.col3
        end) as col3,
       . . .
from dataframe df;

如果您想实际更改数据,那么这很容易适应update.

于 2019-09-05T12:54:38.167 回答