横向解决方法:从查询中的其他位置将参数传递给设置返回函数
在 PostgreSQL 中,我试图加入一个需要查询中其他地方的参数的集合返回函数。如何重写此查询,使其不会产生“对 FROM 子句条目的无效引用?据我了解,所写的查询需要横向支持,
drop table if exists questions;
create table questions (
id int not null primary key,
user_id int not null
);
insert into questions
select generate_series(1,1100), (random()*20000)::int;
drop table if exists users;
create table users (
id int not null primary key
);
insert into users select generate_series(1, 20000);
drop function if exists question_weightings();
create function question_weightings()
returns table (question_id int, weighting int) as $$
select questions.id, (random()*10)::int
from questions;
$$ language sql stable;
drop function if exists similarity(int);
create function similarity(weighting int)
returns table (user_id int, score int) as $$
select users.id, (random() * $1)::int
from users;
$$ language sql stable;
select questions.id, qw.weightings
from questions
join question_weightings() as qw
on qw.question_id = questions.id
join similarity(qw.weighting) as sim
on sim.user_id = questions.user_id;
我怀疑答案就在这个线程中的某个地方: http://archives.postgresql.org/pgsql-general/2011-08 /msg00482.php。 但我尝试过 CTE、子查询、OFFSET 0 等的各种组合,但结果都是空白;每个组合似乎都会永远循环调用相似度(),而不是调用一次并加入它。
In PostgreSQL, I'm trying to join a set-returning function that needs arguments from elsewhere in the query.. how can I rewrite this query so it doesn't yield "invalid reference to FROM-clause entry? As I understand it, the query as written would require LATERAL support, which Postgres doesn't have.
drop table if exists questions;
create table questions (
id int not null primary key,
user_id int not null
);
insert into questions
select generate_series(1,1100), (random()*20000)::int;
drop table if exists users;
create table users (
id int not null primary key
);
insert into users select generate_series(1, 20000);
drop function if exists question_weightings();
create function question_weightings()
returns table (question_id int, weighting int) as $
select questions.id, (random()*10)::int
from questions;
$ language sql stable;
drop function if exists similarity(int);
create function similarity(weighting int)
returns table (user_id int, score int) as $
select users.id, (random() * $1)::int
from users;
$ language sql stable;
select questions.id, qw.weightings
from questions
join question_weightings() as qw
on qw.question_id = questions.id
join similarity(qw.weighting) as sim
on sim.user_id = questions.user_id;
I suspect the answer is somewhere in this thread:
http://archives.postgresql.org/pgsql-general/2011-08/msg00482.php.
But I've played around with various combinations of CTEs, subqueries, OFFSET 0's, etc. and have come up blank; every combination seems to loop forever calling similarity() instead of calling it once and joining it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的示例中有几个问题。
错误消息对此非常清楚:
但还有更多:
STABLE
。weighting
和weightings
。估计是错别字。我将您的演示转变为可行的东西:
也许这一切都可以在较低级别上简化。
You have a couple of issues in your example.
The error message is quite clear on that:
But there is more:
STABLE
.weighting
andweightings
. Presumably typos.I transformed your demo into something that would work:
Maybe this can all be simplified at a lower level.