我有一系列的台词需要合并成一张唱片.Bellow是我的数据的实体模型.有些节号是重复的,我需要将同一节号的几何图形更新到一个记录中.

模型数据:

id section geom
1 32 1234
2 32 1213
3 32 1231
4 33 3121

我需要的是:

id section geom
1 32 1234,1213,1231
4 33 3121

据我所知,ST_union是实现这一点的好方法,但可以找出如何更新行.我想我需要子查询一个更新查询,但我不确定如何格式化它.

示例代码:

UPDATE TABLE "tlbsections"
SELECT section,
       ST_Union(geom) as singlegeom
FROM "Ecorys_alkmaar_sections"
WHERE section = '32'
GROUP BY section
;

推荐答案

有两种 Select

  1. 使用下面的查询将联合记录插入新表-
insert into Ecorys_alkmaar_sections_1 (id,section,geom)
select min(id),section,ST_UNION(geom) as shape 
from Ecorys_alkmaar_sections
group by section
 Output of the above query will be - 

enter image description here

  1. 更新现有表并删除重复记录

您可以使用下面的代码根据连接条件进行更新

update Ecorys_alkmaar_sections t1
set geom = t2.geom 
from (select section,ST_UNION(geom) as shape 
from Ecorys_alkmaar_sections
group by section) t2 
where t1.section=t2.section

上述命令的输出将是-

enter image description here

因此,更新完成后,您需要删除重复记录.

Postgresql相关问答推荐

Postgres SQL:获取列值在连续日期之间已更改的行

如何在PostgreSQL中更改分区的表空间?

使用postgres在go测试容器中使用tern迁移

Postgres从spark触发post-write

为什么我的唯一索引在 Postgresql 中不起作用?

对 Postgres 进行身份验证时如何使用自定义密码哈希算法?

在 SQL 上返回负值

使用 Heroku CLI、Postgres 的 SQL 语法错误

并行取消嵌套多个数组

我应该使用哪个 postgresql 包?

Nodemon - 安装期间clean exit - waiting for changes before restart

PostgreSQL INSERT 插入一个枚举数组

从 psycopg2 异常中获取错误消息

如何 Select 列值为空的行?

Postgresql:在插入列时(或之前)自动小写文本

PostgreSQL:使用 psql 命令行实用程序时 Windows 上的编码问题

在 postgresql 中将 bool 转换为 int

如何在 postgresql 交叉表中用零替换空值

在同一台机器上创建多个 Postgres 实例

Capistrano 与 PostgreSQL,错误:database is being accessed by other users