PostgreSQL:SELECT INTO正在更改字段';s数据类型
我的表格如下:PostgreSQL:SELECT INTO正在更改字段';s数据类型,sql,postgresql,Sql,Postgresql,我的表格如下: CREATE TABLE IF NOT EXISTS foo_raw ( time TIMESTAMPTZ NOT NULL, volume INTEGER, <-- note source field is of type integer price DOUBLE PRECISION ); 然后,我在一个新表中进行选择,将重复的时间行合并为总交易量和交易量加权平均价格 选择进入: SELECT ti
CREATE TABLE IF NOT EXISTS foo_raw
(
time TIMESTAMPTZ NOT NULL,
volume INTEGER, <-- note source field is of type integer
price DOUBLE PRECISION
);
然后,我在一个新表中进行选择,将重复的时间
行合并为总交易量和交易量加权平均价格
选择进入
:
SELECT
time,
SUM(volume)::integer AS volume, <-- note typecast to integer here
SUM(volume * price) / SUM(volume) AS price
INTO
foo
FROM
foo_raw
GROUP BY
time
ORDER BY
time;
类型转换:
SELECT
time,
SUM(volume)::integer AS volume, <-- note typecast to integer here
SUM(volume * price) / SUM(volume) AS price
INTO
foo
FROM
foo_raw
GROUP BY
time
ORDER BY
time;
您将注意到,在我的SELECT-INTO
语句中,我尝试将SUM(volume)
的结果键入整数,但这也不起作用
问题
如何强制字段的类型为integer
?是。使用sum(volume)将其强制转换为整数::类似这样的整数:
SELECT
time,
SUM(volume)::integer AS volume,
here
SUM(volume * price) / SUM(volume) AS price
INTO
foo
FROM
foo_raw
GROUP BY
time
ORDER BY
time;
如果您真正关心结果集的数据类型,我会适当地强制转换每一列。将数字的数据类型更改为更大的值,这样它就不会累积为溢出的数字:
bigint表示smallint或int参数,numeric表示bigint参数,否则与参数数据类型相同
这不正是我已经在做的事情吗?它不起作用了?我遗漏了什么?所以我的整型转换没有效果?我尝试了转换和“选择到”语法和“创建表为…”语法,都制作了一个包含整型的表。这真的很奇怪-我确信我看到了不同的结果…不相关,但是:标准的create table as select
优于专有的select。。进入..
(但这并不能解决你的问题)@a_horse_和_no_的名字谢谢你的提示!
SELECT
time,
SUM(volume)::integer AS volume,
here
SUM(volume * price) / SUM(volume) AS price
INTO
foo
FROM
foo_raw
GROUP BY
time
ORDER BY
time;