如何使用接受浮点数和整数数据类型的列创建 table?
How to create table with a column which accepts float and integer data types?
例如,我有一个只有 2 列的 table:
Table columns
Data type
Date
Datetime
Values
Float32
但是当我想向“值”列插入一个整数时,脚本崩溃了。是的,我知道它只接受 Float32 数据类型。
所以问题是,要选择哪种数据类型,以便“值”列接受浮点值和整数值(如果可能)?
无法从您的问题中获取更多信息。
但我试过Decimal32
的数据类型,然后可以插入整数和浮点数。
Sql是:
-- create local table and distributed table
create table test_decimal on cluster default (name String, value Decimal32(8), created_at Datetime) ENGINE = MergeTree() order by name;
create table test_decimal_d on cluster default (name String, value Decimal32(8), created_at Datetime) ENGINE = Distributed('default', 'default', 'test_decimal', xxHash64(name));
-- insert data
INSERT into test_decimal_d values ('name1', 1.0, '2021-12-17 09:46:00');
INSERT into test_decimal_d values ('name2', 2, '2021-12-17 09:46:01');
例如,我有一个只有 2 列的 table:
Table columns | Data type |
---|---|
Date | Datetime |
Values | Float32 |
但是当我想向“值”列插入一个整数时,脚本崩溃了。是的,我知道它只接受 Float32 数据类型。
所以问题是,要选择哪种数据类型,以便“值”列接受浮点值和整数值(如果可能)?
无法从您的问题中获取更多信息。
但我试过Decimal32
的数据类型,然后可以插入整数和浮点数。
Sql是:
-- create local table and distributed table
create table test_decimal on cluster default (name String, value Decimal32(8), created_at Datetime) ENGINE = MergeTree() order by name;
create table test_decimal_d on cluster default (name String, value Decimal32(8), created_at Datetime) ENGINE = Distributed('default', 'default', 'test_decimal', xxHash64(name));
-- insert data
INSERT into test_decimal_d values ('name1', 1.0, '2021-12-17 09:46:00');
INSERT into test_decimal_d values ('name2', 2, '2021-12-17 09:46:01');