交叉表查询可以在where where condition中有日期吗?

时间:2017-08-01 12:53:40

标签: postgresql crosstab

我正在使用交叉表查询来处理我的数据。但现在我需要根据日期过滤日期,然后在交叉表中转换它。但我认为这是不可能的。我也阅读了postgresql文档,但没有发现任何与之相关的内容。任何人都可以确认一下吗?下面是我试图运行的查询但是出错:

SELECT * FROM crosstab('SELECT a.name, al.type, COALESCE(sum(a.amt),0) as amt FROM account a where date(a.vdate)>='07-30-2017' and date(a.vdate)<='08-31-2017' group by a.name, a.type order by a.name',$$VALUES ('type1'), ('type2')$$) AS ct ("name" text,  "type1" decimal, "type2" decimal))

当我在没有日期的情况下使用它所在的部分时。

提前致谢。

1 个答案:

答案 0 :(得分:2)

对第一个查询使用美元引用,就像您为第二个查询所做的那样:

SELECT * 
FROM crosstab($$
    SELECT a.name, a.type, COALESCE(sum(a.amt),0) as amt 
    FROM account a 
    WHERE date(a.vdate)>='07-30-2017' and date(a.vdate)<='08-31-2017' 
    GROUP BY a.name, a.type 
    ORDER BY a.name
    $$,
    $$
    VALUES ('type1'), ('type2')
    $$
) AS ct ("name" text,  "type1" decimal, "type2" decimal);
相关问题