Postgres在select into语句期间更改数据类型

时间:2016-09-16 16:07:35

标签: sql postgresql postgis type-conversion

使用postgis扩展名运行postgres:尝试在select into table语句期间更改列的数据类型

munsummary表中的sum_popint列是double,我想在select语句中将其更改为整数列。我知道我可以使用update / alter语句在select into语句之前或之后将列数据类型更改为整数,但我想在select语句中执行此操作。

SELECT county,
       SUM(sum_popint) AS residentialpopulation,
       st_union(geom)
INTO countysummary
FROM munsummary
GROUP BY county;

2 个答案:

答案 0 :(得分:2)

你要做的是CAST它是一个整数。这采用CAST ( expression AS type );

的形式

所以在你的SELECT中,试试:

SELECT county,
       CAST(SUM(sum_popint) as INTEGER) AS residentialpopulation,
       st_union(geom)
INTO countysummary
FROM munsummary
GROUP BY county;

答案 1 :(得分:2)

您还可以使用速记语法SUM(sum_popint)::int

SELECT county,
   SUM(sum_popint)::int AS residentialpopulation,
   st_union(geom)
INTO countysummary
FROM munsummary
GROUP BY county