使用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;
答案 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