postgresql合并的简单介绍

postgresql中使用if else语句

1、首先在postgresql数据库中复制表的时候,必须先建立表结构,然后通过insert into语句来实现。

目前创新互联建站已为数千家的企业提供了网站建设、域名、雅安服务器托管网站运营、企业网站设计、南雄网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

2、如果在复制数据之前没有建立相应的表结构,如下图中的错误信息。

3、在查询数据的时候也可以把两个表联合起来进行查询,通过union关键字来进行查询,如下图。

4、如果在使用union进行查询的进修,两条结果一样人被合并成一条。

5、如果想显示联合查询中相同的语句,可以使用union all来进行查询。

【转】PostgreSQL多行变一行

PGSQL多行聚合,PGSQL多行合并为一行,PGSQL多行合并

《PostgreSQL 多行变一行》

《PostgreSQL官方文档-聚集函数》

array_agg(distinct(字段名)) 去重后可以获得拼接唯一的字段

postgresql 合并多张表 ,保留所有不同的列,最后生成一张新表

先不考虑数据库设计的是否合理

你是想新建表,之后把数据存进去?还是建立一个临时表把数据查出来?

首先你两个表要有一定的关联关系,两个表的 SIP一样?DIP一样?还是User一样?

根据一样的作为两个表的关联条件

select * from 表1 t1 left join 表2 t2 on t1.SIP =t2.SIP

PostgreSQL Partition

create_range_partitions

select create_range_partitions('trip_agg_data'::regclass,'time_key',0::bigint,1::bigint,0,false) ;

trip_agg_data --主表OID

time_key --分区列名,必须有not null约束

0::bigint: --起始值 bigint取决于time_key 类型

1::bigint --间隔值 必须大于0

0 --分区个数

false --不立即迁移数据

add_range_partition

select add_range_partition('trip_agg_data'::REGCLASS,0,2019010100,'trip_agg_data_lt_20190101');

2019010100 不能超过time_key类型的最大值

partition_table_concurrently

select partition_table_concurrently ('trip_agg_data_tim'::regclass,10000, 1.0);

迁移数据到子表

10000 --一个事务批量迁移多少记录

1.0 --获得行锁失败时,休眠多久再次获取,重试60次退出任务

pathman_concurrent_part_tasks

select * from pathman_concurrent_part_tasks;

查看数据迁移进度

disable_pathman_for

select disable_pathman_for('trip_agg_data_tim'::regclass);

禁用分区

drop_range_partition

SELECT drop_range_partition(partition, false) /* move data to parent */

FROM pathman_partition_list WHERE parent = 'trip_agg_data'::regclass

and partition = 'trip_agg_data_greater_20200101'::regclass

删除某个分区表

pathman_partition_list

select * from pathman_partition_list;

查看所有分区表信息

set_enable_parent

select set_enable_parent('trip_agg_data'::regclass,false);

禁用主表

split_range_partition

分裂范围分区

merge_range_partitions

合并范围分区

append_range_partition

向后添加范围分区

prepend_range_partition

向前添加范围分区

drop_range_partition

drop_range_partition(partition TEXT, -- 分区名称

delete_data BOOLEAN DEFAULT TRUE) -- 是否删除分区数据,如果false,表示分区数据迁移到主表

drop_partitions

drop_partitions(parent REGCLASS,

delete_data BOOLEAN DEFAULT FALSE)

attach_range_partition

attach_range_partition(relation REGCLASS, -- 主表OID

partition REGCLASS, -- 分区表OID

start_value ANYELEMENT, -- 起始值

end_value ANYELEMENT) -- 结束值

detach_range_partition

detach_range_partition(partition REGCLASS) -- 指定分区名,转换为普通表

-- 1, create table trip_agg_data_2019

create table trip_agg_data_2019 as select * from trip_agg_data where 1=0;

alter table trip_agg_data_2019 alter column time_key set not null;

-- 2, create_range_partition

select create_range_partitions('trip_agg_data_2019'::regclass,'time_key',0::bigint,1::bigint,0,false) ;

-- 3, detach_range_partition for trip_agg_data'

select detach_range_partition('trip_agg_data_20190101');

select detach_range_partition('trip_agg_data_20190201');

select detach_range_partition('trip_agg_data_20190301');

select detach_range_partition('trip_agg_data_20190401');

select detach_range_partition('trip_agg_data_20190501');

select detach_range_partition('trip_agg_data_20190601');

select detach_range_partition('trip_agg_data_20190701');

select detach_range_partition('trip_agg_data_20190801');

select detach_range_partition('trip_agg_data_20190901');

select detach_range_partition('trip_agg_data_20191001');

select detach_range_partition('trip_agg_data_20191101');

select detach_range_partition('trip_agg_data_20191201');

-- 4, add_range_partition for trip_agg_data'

select add_range_partition('trip_agg_data'::REGCLASS,2019010100,2020010100,'trip_agg_data_20190101_20200101');

-- 5, attach_range_partition for trip_agg_data_2019

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190101',2019010100,2019020100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190201',2019020100,2019030100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190301',2019030100,2019040100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190401',2019040100,2019050100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190501',2019050100,2019060100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190601',2019060100,2019070100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190701',2019070100,2019080100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190801',2019080100,2019090100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190901',2019090100,2019100100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20191001',2019100100,2019110100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20191101',2019110100,2019120100);

select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20191201',2019120100,2020010100);

-- 6, disable parent for trip_agg_data_2019

select set_enable_parent('trip_agg_data_2019'::regclass,false);

alter table trip_agg_data alter column time_key set not null;

select create_range_partitions('trip_agg_data'::regclass,'time_key',0::bigint,1::bigint,0,false) ;

select add_range_partition('trip_agg_data'::REGCLASS,0,2019010100,'trip_agg_data_lt_20190101');

select add_range_partition('trip_agg_data'::REGCLASS,2019010100,2019020100,'trip_agg_data_20190101');

select add_range_partition('trip_agg_data'::REGCLASS,2019020100,2019030100,'trip_agg_data_20190201');

select add_range_partition('trip_agg_data'::REGCLASS,2019030100,2019040100,'trip_agg_data_20190301');

select add_range_partition('trip_agg_data'::REGCLASS,2019040100,2019050100,'trip_agg_data_20190401');

select add_range_partition('trip_agg_data'::REGCLASS,2019050100,2019060100,'trip_agg_data_20190501');

select add_range_partition('trip_agg_data'::REGCLASS,2019060100,2019070100,'trip_agg_data_20190601');

select add_range_partition('trip_agg_data'::REGCLASS,2019070100,2019080100,'trip_agg_data_20190701');

select add_range_partition('trip_agg_data'::REGCLASS,2019080100,2019090100,'trip_agg_data_20190801');

select add_range_partition('trip_agg_data'::REGCLASS,2019090100,2019100100,'trip_agg_data_20190901');

select add_range_partition('trip_agg_data'::REGCLASS,2019100100,2019110100,'trip_agg_data_20191001');

select add_range_partition('trip_agg_data'::REGCLASS,2019110100,2019120100,'trip_agg_data_20191101');

select add_range_partition('trip_agg_data'::REGCLASS,2019120100,2020010100,'trip_agg_data_20191201');

select add_range_partition('trip_agg_data'::REGCLASS,2020010100,2020020100,'trip_agg_data_20200101');

select add_range_partition('trip_agg_data'::REGCLASS,2020020100,2020030100,'trip_agg_data_20200201');

select add_range_partition('trip_agg_data'::REGCLASS,2020030100,2020040100,'trip_agg_data_20200301');

select add_range_partition('trip_agg_data'::REGCLASS,2020040100,2020050100,'trip_agg_data_20200401');

select add_range_partition('trip_agg_data'::REGCLASS,2020050100,2020060100,'trip_agg_data_20200501');

select add_range_partition('trip_agg_data'::REGCLASS,2020060100,2020070100,'trip_agg_data_20200601');

select add_range_partition('trip_agg_data'::REGCLASS,2020070100,2020080100,'trip_agg_data_20200701');

select add_range_partition('trip_agg_data'::REGCLASS,2020080100,2020090100,'trip_agg_data_20200801');

select add_range_partition('trip_agg_data'::REGCLASS,2020090100,2020100100,'trip_agg_data_20200901');

select add_range_partition('trip_agg_data'::REGCLASS,2020100100,2020110100,'trip_agg_data_20201001');

select add_range_partition('trip_agg_data'::REGCLASS,2020110100,2020120100,'trip_agg_data_20201101');

select add_range_partition('trip_agg_data'::REGCLASS,2020120100,2021010100,'trip_agg_data_20201201');

select add_range_partition('trip_agg_data'::REGCLASS,2021010100,2021020100,'trip_agg_data_20210101');

select add_range_partition('trip_agg_data'::REGCLASS,2021020100,2021030100,'trip_agg_data_20210201');

select add_range_partition('trip_agg_data'::REGCLASS,2021030100,2021040100,'trip_agg_data_20210301');

select add_range_partition('trip_agg_data'::REGCLASS,2021040100,2021050100,'trip_agg_data_20210401');

select add_range_partition('trip_agg_data'::REGCLASS,2021050100,2021060100,'trip_agg_data_20210501');

select add_range_partition('trip_agg_data'::REGCLASS,2021060100,2021070100,'trip_agg_data_20210601');

select add_range_partition('trip_agg_data'::REGCLASS,2021070100,2021080100,'trip_agg_data_20210701');

select add_range_partition('trip_agg_data'::REGCLASS,2021080100,2021090100,'trip_agg_data_20210801');

select add_range_partition('trip_agg_data'::REGCLASS,2021090100,2021100100,'trip_agg_data_20210901');

select add_range_partition('trip_agg_data'::REGCLASS,2021100100,2021110100,'trip_agg_data_20211001');

select add_range_partition('trip_agg_data'::REGCLASS,2021110100,2021120100,'trip_agg_data_20211101');

select add_range_partition('trip_agg_data'::REGCLASS,2021120100,2022010100,'trip_agg_data_20211201');

select add_range_partition('trip_agg_data'::REGCLASS,2022010100,2022020100,'trip_agg_data_20220101');

select add_range_partition('trip_agg_data'::REGCLASS,2022020100,2022030100,'trip_agg_data_20220201');

select add_range_partition('trip_agg_data'::REGCLASS,2022030100,2022040100,'trip_agg_data_20220301');

select add_range_partition('trip_agg_data'::REGCLASS,2022040100,2022050100,'trip_agg_data_20220401');

select add_range_partition('trip_agg_data'::REGCLASS,2022050100,2022060100,'trip_agg_data_20220501');

select add_range_partition('trip_agg_data'::REGCLASS,2022060100,2022070100,'trip_agg_data_20220601');

select add_range_partition('trip_agg_data'::REGCLASS,2022070100,2022080100,'trip_agg_data_20220701');

select add_range_partition('trip_agg_data'::REGCLASS,2022080100,2022090100,'trip_agg_data_20220801');

select add_range_partition('trip_agg_data'::REGCLASS,2022090100,2022100100,'trip_agg_data_20220901');

select add_range_partition('trip_agg_data'::REGCLASS,2022100100,2022110100,'trip_agg_data_20221001');

select add_range_partition('trip_agg_data'::REGCLASS,2022110100,2022120100,'trip_agg_data_20221101');

select add_range_partition('trip_agg_data'::REGCLASS,2022120100,2023010100,'trip_agg_data_20221201');

select add_range_partition('trip_agg_data'::REGCLASS,2023010100,2023020100,'trip_agg_data_20230101');

select add_range_partition('trip_agg_data'::REGCLASS,2023020100,2023030100,'trip_agg_data_20230201');

select add_range_partition('trip_agg_data'::REGCLASS,2023030100,2023040100,'trip_agg_data_20230301');

select add_range_partition('trip_agg_data'::REGCLASS,2023040100,2023050100,'trip_agg_data_20230401');

select add_range_partition('trip_agg_data'::REGCLASS,2023050100,2023060100,'trip_agg_data_20230501');

select add_range_partition('trip_agg_data'::REGCLASS,2023060100,2023070100,'trip_agg_data_20230601');

select add_range_partition('trip_agg_data'::REGCLASS,2023070100,2023080100,'trip_agg_data_20230701');

select add_range_partition('trip_agg_data'::REGCLASS,2023080100,2023090100,'trip_agg_data_20230801');

select add_range_partition('trip_agg_data'::REGCLASS,2023090100,2023100100,'trip_agg_data_20230901');

select add_range_partition('trip_agg_data'::REGCLASS,2023100100,2023110100,'trip_agg_data_20231001');

select add_range_partition('trip_agg_data'::REGCLASS,2023110100,2023120100,'trip_agg_data_20231101');

select add_range_partition('trip_agg_data'::REGCLASS,2023120100,2024010100,'trip_agg_data_20231201');

select add_range_partition('trip_agg_data'::REGCLASS,2024010100,2024020100,'trip_agg_data_20240101');

select add_range_partition('trip_agg_data'::REGCLASS,2024020100,2024030100,'trip_agg_data_20240201');

select add_range_partition('trip_agg_data'::REGCLASS,2024030100,2024040100,'trip_agg_data_20240301');

select add_range_partition('trip_agg_data'::REGCLASS,2024040100,2024050100,'trip_agg_data_20240401');

select add_range_partition('trip_agg_data'::REGCLASS,2024050100,2024060100,'trip_agg_data_20240501');

select add_range_partition('trip_agg_data'::REGCLASS,2024060100,2024070100,'trip_agg_data_20240601');

select add_range_partition('trip_agg_data'::REGCLASS,2024070100,2024080100,'trip_agg_data_20240701');

select add_range_partition('trip_agg_data'::REGCLASS,2024080100,2024090100,'trip_agg_data_20240801');

select add_range_partition('trip_agg_data'::REGCLASS,2024090100,2024100100,'trip_agg_data_20240901');

select add_range_partition('trip_agg_data'::REGCLASS,2024100100,2024110100,'trip_agg_data_20241001');

select add_range_partition('trip_agg_data'::REGCLASS,2024110100,2024120100,'trip_agg_data_20241101');

select add_range_partition('trip_agg_data'::REGCLASS,2024120100,2025010100,'trip_agg_data_20241201');

select add_range_partition('trip_agg_data'::REGCLASS,2025010100,2025020100,'trip_agg_data_20250101');

select add_range_partition('trip_agg_data'::REGCLASS,2025020100,2025030100,'trip_agg_data_20250201');

select add_range_partition('trip_agg_data'::REGCLASS,2025030100,2025040100,'trip_agg_data_20250301');

select add_range_partition('trip_agg_data'::REGCLASS,2025040100,2025050100,'trip_agg_data_20250401');

select add_range_partition('trip_agg_data'::REGCLASS,2025050100,2025060100,'trip_agg_data_20250501');

select add_range_partition('trip_agg_data'::REGCLASS,2025060100,2025070100,'trip_agg_data_20250601');

select add_range_partition('trip_agg_data'::REGCLASS,2025070100,2025080100,'trip_agg_data_20250701');

select add_range_partition('trip_agg_data'::REGCLASS,2025080100,2025090100,'trip_agg_data_20250801');

select add_range_partition('trip_agg_data'::REGCLASS,2025090100,2025100100,'trip_agg_data_20250901');

select add_range_partition('trip_agg_data'::REGCLASS,2025100100,2025110100,'trip_agg_data_20251001');

select add_range_partition('trip_agg_data'::REGCLASS,2025110100,2025120100,'trip_agg_data_20251101');

select add_range_partition('trip_agg_data'::REGCLASS,2025120100,2026010100,'trip_agg_data_20251201');

select add_range_partition('trip_agg_data'::REGCLASS,2026010100,2026020100,'trip_agg_data_20260101');

select add_range_partition('trip_agg_data'::REGCLASS,2026020100,2026030100,'trip_agg_data_20260201');

select add_range_partition('trip_agg_data'::REGCLASS,2026030100,2026040100,'trip_agg_data_20260301');

select add_range_partition('trip_agg_data'::REGCLASS,2026040100,2026050100,'trip_agg_data_20260401');

select add_range_partition('trip_agg_data'::REGCLASS,2026050100,2026060100,'trip_agg_data_20260501');

select add_range_partition('trip_agg_data'::REGCLASS,2026060100,2026070100,'trip_agg_data_20260601');

select add_range_partition('trip_agg_data'::REGCLASS,2026070100,2026080100,'trip_agg_data_20260701');

select add_range_partition('trip_agg_data'::REGCLASS,2026080100,2026090100,'trip_agg_data_20260801');

select add_range_partition('trip_agg_data'::REGCLASS,2026090100,2026100100,'trip_agg_data_20260901');

select add_range_partition('trip_agg_data'::REGCLASS,2026100100,2026110100,'trip_agg_data_20261001');

select add_range_partition('trip_agg_data'::REGCLASS,2026110100,2026120100,'trip_agg_data_20261101');

select add_range_partition('trip_agg_data'::REGCLASS,2026120100,2027010100,'trip_agg_data_20261201');

select add_range_partition('trip_agg_data'::REGCLASS,2027010100,2027020100,'trip_agg_data_20270101');

select add_range_partition('trip_agg_data'::REGCLASS,2027020100,2027030100,'trip_agg_data_20270201');

select add_range_partition('trip_agg_data'::REGCLASS,2027030100,2027040100,'trip_agg_data_20270301');

select add_range_partition('trip_agg_data'::REGCLASS,2027040100,2027050100,'trip_agg_data_20270401');

select add_range_partition('trip_agg_data'::REGCLASS,2027050100,2027060100,'trip_agg_data_20270501');

select add_range_partition('trip_agg_data'::REGCLASS,2027060100,2027070100,'trip_agg_data_20270601');

select add_range_partition('trip_agg_data'::REGCLASS,2027070100,2027080100,'trip_agg_data_20270701');

select add_range_partition('trip_agg_data'::REGCLASS,2027080100,2027090100,'trip_agg_data_20270801');

select add_range_partition('trip_agg_data'::REGCLASS,2027090100,2027100100,'trip_agg_data_20270901');

select add_range_partition('trip_agg_data'::REGCLASS,2027100100,2027110100,'trip_agg_data_20271001');

select add_range_partition('trip_agg_data'::REGCLASS,2027110100,2027120100,'trip_agg_data_20271101');

select add_range_partition('trip_agg_data'::REGCLASS,2027120100,2028010100,'trip_agg_data_20271201');

select add_range_partition('trip_agg_data'::REGCLASS,2028010100,2028020100,'trip_agg_data_20280101');

select add_range_partition('trip_agg_data'::REGCLASS,2028020100,2028030100,'trip_agg_data_20280201');

select add_range_partition('trip_agg_data'::REGCLASS,2028030100,2028040100,'trip_agg_data_20280301');

select add_range_partition('trip_agg_data'::REGCLASS,2028040100,2028050100,'trip_agg_data_20280401');

select add_range_partition('trip_agg_data'::REGCLASS,2028050100,2028060100,'trip_agg_data_20280501');

select add_range_partition('trip_agg_data'::REGCLASS,2028060100,2028070100,'trip_agg_data_20280601');

select add_range_partition('trip_agg_data'::REGCLASS,2028070100,2028080100,'trip_agg_data_20280701');

select add_range_partition('trip_agg_data'::REGCLASS,2028080100,2028090100,'trip_agg_data_20280801');

select add_range_partition('trip_agg_data'::REGCLASS,2028090100,2028100100,'trip_agg_data_20280901');

select add_range_partition('trip_agg_data'::REGCLASS,2028100100,2028110100,'trip_agg_data_20281001');

select add_range_partition('trip_agg_data'::REGCLASS,2028110100,2028120100,'trip_agg_data_20281101');

select add_range_partition('trip_agg_data'::REGCLASS,2028120100,2029010100,'trip_agg_data_20281201');

select add_range_partition('trip_agg_data'::REGCLASS,2029010100,2029020100,'trip_agg_data_20290101');

select add_range_partition('trip_agg_data'::REGCLASS,2029020100,2029030100,'trip_agg_data_20290201');

select add_range_partition('trip_agg_data'::REGCLASS,2029030100,2029040100,'trip_agg_data_20290301');

select add_range_partition('trip_agg_data'::REGCLASS,2029040100,2029050100,'trip_agg_data_20290401');

select add_range_partition('trip_agg_data'::REGCLASS,2029050100,2029060100,'trip_agg_data_20290501');

select add_range_partition('trip_agg_data'::REGCLASS,2029060100,2029070100,'trip_agg_data_20290601');

select add_range_partition('trip_agg_data'::REGCLASS,2029070100,2029080100,'trip_agg_data_20290701');

select add_range_partition('trip_agg_data'::REGCLASS,2029080100,2029090100,'trip_agg_data_20290801');

select add_range_partition('trip_agg_data'::REGCLASS,2029090100,2029100100,'trip_agg_data_20290901');

select add_range_partition('trip_agg_data'::REGCLASS,2029100100,2029110100,'trip_agg_data_20291001');

select add_range_partition('trip_agg_data'::REGCLASS,2029110100,2029120100,'trip_agg_data_20291101');

select add_range_partition('trip_agg_data'::REGCLASS,2029120100,2030010100,'trip_agg_data_20291201');

select add_range_partition('trip_agg_data'::REGCLASS,2030010100,2147483647,'trip_agg_data_gt_20300101');

select partition_table_concurrently ('trip_agg_data'::regclass,10000, 1.0);

select set_enable_parent('trip_agg_data'::regclass,false);


标题名称:postgresql合并的简单介绍
网站地址:http://scyanting.com/article/dscgoph.html