postgresql - UNION ALL,将 NULL 转换为 double ,Postgres

标签 postgresql

在 postgres 中,当我尝试合并两个表时出现错误,其中一个表的列 (Amount) 包含 double precision 数据类型,并且其他表没有匹配的列,我希望该表中的记录在 Amount 字段中只有 NULL

错误:

"union types text and double precision cannot be matched postgres"

伪代码:

SELECT * FROM (

SELECT
   t1.Amount AS 'amount',
   NULL::DATE AS 'date'
FROM Table1 AS t1
UNION ALL

SELECT
   /* next line is the issue */
   NULL AS 'amount',
   t2.Date AS 'date'
FROM Table2 AS t2

) AS FOO

我相当确定这个解决方案是一个简单的转换问题,但无法通过搜索找到任何内容。我如何在 postgres 中执行与 NULL::DOUBLE 等效的操作?

编辑::后代

@klin 和@a_horse_with_no_name 的评论接受的答案指向一个“历史”postgres 转换表达式::,其中 syntax is equivalent :

CAST ( expression AS type )  
expression::type

而且,这是一个列表 postgres data types .

最佳答案

在 Postgres 中,double precision 类型也称为 float8 或简称为 float。使用其中之一。

select null::double precision, null::float;

关于postgresql - UNION ALL,将 NULL 转换为 double ,Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41864212/

相关文章:

ruby-on-rails - Postgres查询查找数据库是否为只读模式

sql - PostgreSQL 条件,其中日期 > "first day of the month"

arrays - 具有可选子集的无序集(数组)上的匹配表

sql - 如何将多个重复行转换为 SQL (Postgres) 中的数组?

python - 使用 Postgresql 在 Google App Engine 上使用 SQLAlchemy 和 pg8000 KeyError/Broken Pipe

sql - PostgreSQL 9.1 主键自增

python - psycopg2 类型施法者已创建并注册但未找到

sql - ActiveRecord::Base.connection 和 find_by_sql 之间的区别

sql - 改进此 SQL 查询的方法

ruby-on-rails - 在代码中使用模型和迁移