我有一个数据管道从Postgres中提取大量数据并将其推送到数据显示工具中。我的典型流程看起来像这样,并且工作正常:
create temporary table foo as
select w.id, w.name, w.created_at
from waldo w;
\copy "foo" to './foo.txt';
然后我使用那个转出的foo.txt文件。 (实际的查询要复杂得多,并且在十几个其他表上加入和分组,但你明白了。)
我开发了一个使用" with query"我无法弄清楚将结果导入文本文件的语法。此查询在我的交互式SQL窗口中正常运行:
WITH daze
AS (SELECT ( CURRENT_DATE - b.offs ) AS date
FROM (SELECT Generate_series(0, CURRENT_DATE - '2015-01-15', 1) AS
offs)
AS b)
select w.id, w.name, w.created_at
from waldo w, daze d
where d.date + 1 > w.created_at
但我需要将其添加到我的每日转储脚本中,并获取一个可以转储的临时表对象。而且这段代码没有运行:
create temporary table foo as
WITH daze
AS (SELECT ( CURRENT_DATE - b.offs ) AS date
FROM (SELECT Generate_series(0, CURRENT_DATE - '2015-01-15', 1) AS
offs)
AS b)
select w.id, w.name, w.created_at
from waldo w, daze d
where d.date + 1 > w.created_at
我挣扎的语法是什么?提前谢谢。
答案 0 :(得分:0)
抱歉,我不知道Postgres,只是TSQL,但你可以尝试在#34; daze"下面宣布另一个CTE。一个在同一个WITH里面?
此页面说明了Postgres语法中的可能性。 http://www.postgresql.org/docs/9.1/static/queries-with.html
WITH daze
AS (SELECT ( CURRENT_DATE - b.offs ) AS date
FROM (SELECT Generate_series(0, CURRENT_DATE - '2015-01-15', 1) AS
offs)
AS b)
,foo
as (select w.id, w.name, w.created_at
from waldo w, daze d
where d.date + 1 > w.created_at)
select * from foo
答案 1 :(得分:0)
您的整个CTE是一种非常复杂的做法:
index.html
为什么不简单地说:
generate_series(CURRENT_DATE::timestamp, '2015-01-15'::timestamp, interval '-1 day') AS date
(请注意,CREATE TEMPORARY TABLE foo AS
SELECT w.id, w.name, w.created_at
FROM waldo w,
generate_series((CURRENT_DATE + 1)::timestamp, '2015-01-16'::timestamp, interval '-1 day') AS d(date)
WHERE d.date > w.created_at;
条款中的+ 1
已在WHERE
来电中吸收。)