简体   繁体   English

在 timescaledb 中使用不同的列名和列顺序将数据从一个表复制到另一个表

[英]Copy data from one table to another with different column names and column ordering in timescaledb

I have 2 timescaledb tables with different order of columns and even different column names for for some.我有 2 个 timescaledb 表,它们的列顺序不同,有些甚至有不同的列名。 Is it possible for me to copy data from one table to another table?我可以将数据从一个表复制到另一个表吗?

Essentially the new table has hyper table on it, but the old one does not have hyper table on it.本质上,新表上有 hyper table,而旧表上没有 hyper table。

I have looked at https://docs.timescale.com/timescaledb/latest/how-to-guides/migrate-data/same-db/ However it only seems to tell i need to have same column names and even the same column order in the create table syntax.我看过https://docs.timescale.com/timescaledb/latest/how-to-guides/migrate-data/same-db/但是它似乎只告诉我需要具有相同的列名甚至是同一列创建表语法中的顺序。 Can you assist, am new to timescaledb你能帮忙吗,我是 timescaledb 的新手

eg:例如:

**table1**
id 
price
datetime_string


**table2**
id 
time
price

I created a minimal example here:我在这里创建了一个最小的例子:

CREATE TABLE old_table ( id bigserial, time_string text NOT NULL, price decimal);
CREATE TABLE new_table ( time TIMESTAMP NOT NULL,  price decimal);
SELECT create_hypertable('new_table', 'time');


INSERT INTO old_table (time_string, price) VALUES
('2021-08-26 10:09:00.01', 10.1),
('2021-08-26 10:09:00.08',  10.0),
('2021-08-26 10:09:00.23',  10.2),
('2021-08-26 10:09:00.40',  10.3);
INSERT INTO new_table SELECT time_string::timestamp as time, price from old_table;

Results:结果:

playground=# \i move_data.sql
CREATE TABLE
CREATE TABLE
┌─────────────────────────┐
│    create_hypertable    │
├─────────────────────────┤
│ (19,public,new_table,t) │
└─────────────────────────┘
(1 row)

INSERT 0 4
INSERT 0 4
playground=# table new_table;
┌────────────────────────┬───────┐
│          time          │ price │
├────────────────────────┼───────┤
│ 2021-08-26 10:09:00.01 │  10.1 │
│ 2021-08-26 10:09:00.08 │  10.0 │
│ 2021-08-26 10:09:00.23 │  10.2 │
│ 2021-08-26 10:09:00.4  │  10.3 │
└────────────────────────┴───────┘
(4 rows)

Can you try to execute your select first and see if the relation matches the table structure?你可以先尝试执行你的 select 看看关系是否匹配表结构?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从一个 PostgreSQL 数据库迁移到另一个(表/列名略有不同)? - How can I migrate data from one PostgreSQL database to another (with slightly different table/column names)? SQL - 在同一个表中将数据从一列复制到另一列 - SQL - Copy data from one column to another in the same table 如何从 DBeaver 中的表中复制所有列名和数据类型? - How to copy all column names and data types from table in DBeaver? Postgres将数据从一个表插入到另一列名称匹配的表 - Postgres insert data from one table to another where column names match 在ON CONFLICT中将行列从一个表复制到另一个表 - Copy a row column from one table to another in ON CONFLICT 如何使用联接将列从一个表复制到另一个表 - How to copy column from one table to another using JOIN 如何将列数据从一个表分离到另一个表? - How to separate column data to copy from one table to other table? 创建触发器以将数据从同一列中的一列复制到另一列-PostgreSQL - Creating a trigger to copy data from one column to another in the same table - PostgreSQL 通过在PSQL中进行修改将数据从列插入到另一个不同的表列 - Insert data from column to another different table column with modifying in PSQL Postgres SQL - 将数据从一列复制到另一列,如果为空,则从另一列复制 - Postgres SQL - copy data from one column to another and if null from another one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM