テーブルの結果を返す 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
...しかし、それはERROR: function expression in FROM cannot refer to other Relations of same query levelで失敗し、メインクエリをサブクエリに入れても役に立ちません。