将多列从 unix 秒转换为时间戳,并将其添加到现有 table 作为 Postgresql 中的两个新列
Convert multiple columns to timestamp from unix seconds and add it to the existing table as two new columns in Postgresql
我是 postgresql 的新手,我正在尝试将两列从 unix 秒转换为时间戳并添加为新列。但我不确定如何纠正这个问题。
SELECT arrival_unix_seconds,departure_unix_seconds,to_timestamp(arrival_unix_seconds) as arrival FROM public."Operation"
alter table add column arrival timestamp;
我只能对一列执行此操作并仅显示结果但无法将其添加到 table。
还想找出两个结果列之间的时间差(以分钟为单位)。
数据看起来像,
arrival_unix_seconds departure_unix_seconds
1619808731; 1619809039;
1619808082; 1619808711;
1619807810; 1619809705;
1619807573; 1619808556;
1619807394; 1619808623;
您可以使用以下查询来解决您的问题
alter table public."Operation" add column arrival timestamp;
update public."Operation" set arrival=to_timestamp(arrival_unix_seconds);
alter table public."Operation" add column departure timestamp;
update public."Operation" set departure=to_timestamp(departure_unix_seconds);
要计算以分钟为单位的差异,您可以使用以下方法:
如果您想保留 departure_unix_seconds 和 arrival_unix_seconds
列,则
select (departure_unix_seconds - arrival_unix_seconds)/60 "Difference" from public."Operation";
如果您想使用新创建的列,那么
select extract( epoch from (departure - arrival))::integer/60 "Difference" from public."Operation";
首先更改 table 并添加两列。
ALTER TABLE public."Operation"
ADD COLUMN arrival timestamp,
ADD COLUMN departure timestamp;
然后使用UPDATE
将转换后的时间戳复制到新列中。
UPDATE public."Operation"
SET arrival = to_timestamp(arrival_unix_seconds),
departure = to_timestamp(departure_unix_seconds);
并且,由于您现在有直接依赖于 table 中其他列的列,您应该删除旧列以再次规范化 table。
ALTER TABLE public."Operation"
DROP COLUMN arrival_unix_seconds,
DROP COLUMN departure_unix_seconds;
您还应该考虑不要使用区分大小写的标识符,例如 table 名称。他们只会让事情变得比必要的更复杂。数据库中的标识符不需要“漂亮”。这是表示层的工作。
我是 postgresql 的新手,我正在尝试将两列从 unix 秒转换为时间戳并添加为新列。但我不确定如何纠正这个问题。
SELECT arrival_unix_seconds,departure_unix_seconds,to_timestamp(arrival_unix_seconds) as arrival FROM public."Operation"
alter table add column arrival timestamp;
我只能对一列执行此操作并仅显示结果但无法将其添加到 table。
还想找出两个结果列之间的时间差(以分钟为单位)。
数据看起来像,
arrival_unix_seconds departure_unix_seconds
1619808731; 1619809039;
1619808082; 1619808711;
1619807810; 1619809705;
1619807573; 1619808556;
1619807394; 1619808623;
您可以使用以下查询来解决您的问题
alter table public."Operation" add column arrival timestamp;
update public."Operation" set arrival=to_timestamp(arrival_unix_seconds);
alter table public."Operation" add column departure timestamp;
update public."Operation" set departure=to_timestamp(departure_unix_seconds);
要计算以分钟为单位的差异,您可以使用以下方法:
如果您想保留 departure_unix_seconds 和 arrival_unix_seconds
列,则
select (departure_unix_seconds - arrival_unix_seconds)/60 "Difference" from public."Operation";
如果您想使用新创建的列,那么
select extract( epoch from (departure - arrival))::integer/60 "Difference" from public."Operation";
首先更改 table 并添加两列。
ALTER TABLE public."Operation"
ADD COLUMN arrival timestamp,
ADD COLUMN departure timestamp;
然后使用UPDATE
将转换后的时间戳复制到新列中。
UPDATE public."Operation"
SET arrival = to_timestamp(arrival_unix_seconds),
departure = to_timestamp(departure_unix_seconds);
并且,由于您现在有直接依赖于 table 中其他列的列,您应该删除旧列以再次规范化 table。
ALTER TABLE public."Operation"
DROP COLUMN arrival_unix_seconds,
DROP COLUMN departure_unix_seconds;
您还应该考虑不要使用区分大小写的标识符,例如 table 名称。他们只会让事情变得比必要的更复杂。数据库中的标识符不需要“漂亮”。这是表示层的工作。