我试图在我的sql语句中调用Postgres中的函数。它给了我一个"列xxx未找到"错误。我的SQL代码如下。
SELECT r.resource_id, r.name, owner.name, r.status,
r.cost, r.display_unit, r.next_available_date,
geodistance(r.latitude, r.longitude, 41.824, 71.4128) as distance
FROM resource_item as r
INNER JOIN base_user AS owner
ON r.resource_owner = owner.username
WHERE distance < 100
ORDER BY distance, r.next_avail_date, r.name;
我收到以下错误:
ProgrammingError: column "distance" does not exist
函数geodistance()本身已作为单个语句进行测试,如下所示,并且工作正常。
select geodistance(38.898556, -77.037852, 38.897147, -77.043934) as distance from incident;
如果我将geodistance()函数放在WHERE子句中,我可以得到响应,但我不知道如何进行ORDER BY。
我被困了一下。我需要评估距离并能够与输入值进行比较。我怎样才能做到这一点?
答案 0 :(得分:1)
您不能在同一级别查询的WHERE
子句中将label用作列名。
CREATE OR REPLACE FUNCTION public.one()
RETURNS integer
LANGUAGE sql
AS $function$ select 1 $function$
postgres=# select one() as fooa from generate_series(1,3) where one < 10;
ERROR: column "one" does not exist
LINE 1: ...lect one() as fooa from generate_series(1,3) where one < 10;
您必须使用子查询
postgres=# select * from (select one() as fooa from generate_series(1,3)) s where fooa < 10;
+------+
| fooa |
+------+
| 1 |
| 1 |
| 1 |
+------+
(3 rows)