我在 PostgreSQL 9.0 中有一些返回表结果的函数。这些背后的想法是返回特定时间的数据,例如。
CREATE FUNCTION person_asof(effective_time timestamp with time zone)
RETURNS SETOF person
...
CREATE FUNCTION pgroup_asof(effective_time timestamp with time zone)
RETURNS SETOF pgroup
...
我几乎可以像查询表一样查询它们,包括连接和所有:
SELECT *
FROM pgroup_asof('2011-01-01') g
JOIN person_asof('2011-01-01') p
ON g.id = p.group_id
这很好用,但是有什么技巧可以用来指定一次有效时间吗?
我试图做这样的事情:
SELECT *
FROM (SELECT '2010-04-12'::timestamp ts) effective,
pgroup_asof(effective.ts) g
JOIN person_asof(effective.ts) p
ON g.id = p.group_id
...但是失败并出现错误:FROM 中的函数表达式不能引用相同查询级别的其他关系,并且将主查询放入子查询也无济于事。