【问题标题】:Postgres change datatype during select into statementPostgres 在 select into 语句期间更改数据类型
【发布时间】:2017-01-24 22:36:36
【问题描述】:

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

【问题讨论】:

    标签: sql postgresql postgis type-conversion


    【解决方案1】:

    您也可以使用简写语法SUM(sum_popint)::int

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

    【讨论】:

    • 我曾考虑使用简写,但认为 CAST 可能更有用,因为它是 ansi sql。
    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 2011-01-22
      • 1970-01-01
      相关资源
      最近更新 更多