【发布时间】:2015-12-03 22:56:46
【问题描述】:
我创建的一些 postgres 代码给了我一个错误:
ERROR: CASE types character varying and numeric cannot be matched
代码:
CREATE TABLE current_condition_joined AS SELECT
a.id, a.geom, a.condition_join_1, a.condition_join_2, a.condition_join_3,
(CASE WHEN b.condition = 'ERROR' THEN (CASE WHEN c.condition2 = 'ERROR' THEN d.condition3
ELSE c.condition2
END)
ELSE b.condition
END) current_condition,
(CASE WHEN b.condition= 'ERROR' THEN (CASE WHEN c.condition2 = 'ERROR' THEN d.ecosite3
ELSE c.ecosite2
END)
ELSE b.ecosite
END) current_ecosite,
(CASE WHEN b.condition = 'ERROR' THEN (CASE WHEN c.condition2 = 'ERROR' THEN d.ecophase3
ELSE c.ecophase2
END)
ELSE b.ecophase
END) current_ecophase,
(CASE WHEN b.condition = 'ERROR' THEN (CASE WHEN c.condition2 = 'ERROR' THEN d.consite3
ELSE c.consite2
END)
ELSE b.consite
END) current_consite,
(CASE WHEN b.condition = 'ERROR' THEN (CASE WHEN c.condition2 = 'ERROR' THEN d.conphase3
ELSE c.conphase2
END)
ELSE b.conphase
END) current_conphase
FROM current_condition a, boreal_mixedwood_labeled b, boreal_mixedwood_labeled c, boreal_mixedwood_labeled d
WHERE a.label = b.label_join_1
and a.label2 = c.label_join_2
and a.label3 = d.label_join_3;
b、c、d ecosite 和 phase 都是数字类型。唯一的条件是 varchar。
在第二列和第三列中,出现问题的地方是创作。我假设我得到了错误,因为它在引用 varchar 的情况下的第一部分,但第二种情况的结果是数字。我想使用条件“错误”来选择要使用的数值。
我是 postgres (9.4.5) 的新手,但对 sql 相当精通。我在 Windows 机器上的 pgAdmin (v. 1.18.1) 中工作。
我查看了我的问题的其他实例,但他们不考虑嵌套语句。 What's wrong with my CASE?
在 PGAdmin 的 SQL 窗格中创建表 current_condition 的代码:
CREATE TABLE current_condition (
geom geometry,
condition_join_1 text,
condition_join_2 text,
condition_join_3 text,
id serial NOT NULL,
CONSTRAINT current_condition_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_current_condition_geom
ON current_condition USING gist (geom);
boreal_mixedwood_labeled 表的代码:
CREATE TABLE boreal_mixedwood_labeled
(
objectid serial NOT NULL,
label character varying(255),
label2 character varying(255),
label3 character varying(255),
condition character varying(255),
ecophase numeric(15,6),
ecosite numeric(15,6),
conphase character varying(255),
consite character varying(255),
condition2 character varying(255),
ecophase2 numeric(15,6),
ecosite2 numeric(15,6),
conphase2 character varying(255),
consite2 character varying(255),
condition3 character varying(255),
ecophase3 numeric(15,6),
ecosite3 numeric(15,6),
conphase3 character varying(255),
consite3 character varying(255),
CONSTRAINT boreal_mixedwood_labeled_pkey PRIMARY KEY (objectid)
ERWIN 的回答是正确的。虽然列中的值是数字,但表格出于某种原因将它们作为字符。一定是从我的导入中自动发生的。
【问题讨论】:
-
您忘记了
geom和您的 Postgres 版本的表限定条件。请将两者都添加到问题中。理想情况下,您将添加表定义以快速澄清事情(您在 psql 中使用\d tbl得到什么)。 -
抱歉,postgres 的新手...geom 的表限定是什么意思??
-
geom来自哪个表?你有a.id, geom, a.condition_join_1, ...所以id和condition_join_1来自a。还有geom?更一般地说,如果你提供表定义,我可以给你一个更简单的查询,而不是玩这个猜谜游戏。由于您使用的是 pgAdmin:您也可以从 SQL 窗格中复制CREATE TABLE脚本。 -
geom 应该来自一个。那是唯一存在的位置几何。 boreal_mixedwood 表是非空间表。为 current_condition 添加了 sql 窗格
-
快到了。我删除了不相关的部分。你能为
boreal_mixedwood_labeled提供同样的服务吗?
标签: sql postgresql pgadmin