[英]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.