Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 按列移动数据_Postgresql - Fatal编程技术网

Postgresql 按列移动数据

Postgresql 按列移动数据,postgresql,Postgresql,我有一个带有日期列的表,例如: a b c d e 09-09-2015 09-08-2015 09-12-2015 09-08-2015 09-11-2015 09-12-2015 09-08-2015 09-11-2015 09-12-2015 09-08-2015 09-

我有一个带有日期列的表,例如:

a           b           c           d           e  
            09-09-2015  09-08-2015              09-12-2015
09-08-2015              09-11-2015  09-12-2015  
09-08-2015              09-11-2015              09-12-2015
09-08-2015  09-09-2015              09-11-2015  09-12-2015
09-08-2015  09-09-2015  09-10-2015  09-11-2015  09-12-2015
是否有一种方法可以创建一个新表,其中所有日期值都向左移动,因此没有间隙?对于我的数据,这将是:

a           b           c           d           e  
09-09-2015  09-08-2015  09-12-2015
09-08-2015  09-11-2015  09-12-2015  
09-08-2015  09-11-2015  09-12-2015
09-08-2015  09-09-2015  09-11-2015  09-12-2015
09-08-2015  09-09-2015  09-10-2015  09-11-2015  09-12-2015
样本数据:

-- drop table if exists tst;

create table tst (
    a date,
    b date,
    c date,
    d date,
    e date);

insert into tst values 
    (NULL, current_date + 1, current_date, NULL, current_date + 4),
    (current_date, NULL, current_date + 3, current_date + 4, NULL),
    (current_date, NULL, current_date + 3, NULL, current_date + 4),
    (current_date, current_date + 1, NULL, current_date + 3, current_date + 4),
    (current_date, current_date + 1, current_date + 2, current_date + 3, current_date  + 4)     
    ;
我认为它将与简单的更新一起工作:

update tst set a = case when a is not null then a else
            case    when b is not null then b
                when c is not null then c
                when d is not null then d
                when e is not null then e end
            end;

这不是一种方法,因为对于列
b
,我需要处理以前将值移动到
a
的情况。

如果可以创建新表,则可以将
tst
的每一行转换为只包含日期数组的行。然后您可以使用
array\u remove(anyarray,NULL)
删除
NULL
s,并用结果数组填充新表

CREATE OR REPLACE VIEW tst_array AS
    SELECT
        array_remove(ARRAY[a,b,c,d,e], NULL) AS dates
    FROM
        tst;

SELECT
    dates[1] AS a,
    dates[2] AS b,
    dates[3] AS c,
    dates[4] AS d,
    dates[5] AS e
FROM
    tst_array;

这很酷。当我继续使用PostgreSQL时,没有
array\u remove()
@TomasGreif:
array\u remove
是在9.3中引入的