Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql Postgres中的交叉表_Postgresql_Crosstab - Fatal编程技术网

Postgresql Postgres中的交叉表

Postgresql Postgres中的交叉表,postgresql,crosstab,Postgresql,Crosstab,我正试图在Postgres中调整一张表格。我的表“sample”有许多列:code、sector、concept、year、period、value、preorder,而不是每行有1个concept、1year、1个value,我希望概念按年份包含值 Select * from crosstab('Select concept, year, value from sample_table Where code=1800 AND period='Annual' AND Se

我正试图在Postgres中调整一张表格。我的表“sample”有许多列:code、sector、concept、year、period、value、preorder,而不是每行有1个concept、1year、1个value,我希望概念按年份包含值

Select * from crosstab('Select concept, year, value from sample_table
    Where code=1800
    AND period='Annual'
    AND Sector='Agro'
    Order by year,preorder;
    ',
    $$VALUES ('2009'::int), ('2010'), ('2011'), ('2012'), ('2013'), ('2014')$$)
AS value("concept" varchar(255),"2009" int, "2010" int, "2011" int, "2012" int, "2013" int, "2014" int);
首先,我犯了一个错误:

ERROR:  syntax error at or near "Annual"
LINE 3:  AND period='Annual'
然后,当我用双引号替换单引号时,我得到:

ERROR:  column "Annual" does not exist
LINE 3:  AND period="Annual"
                         ^
你知道我做错了什么吗


第二个问题是,是否有人知道如何动态生成列名,也就是说,与其硬编码年份,不如在相关列上执行select distinct?

您的内部引号没有正确转义。请尝试使用$$而不是'。双引号与SQL中的单引号不同;它们专门用于引用标识符列名、表名等,而不是字符串。Thx,我看到的大多数示例似乎都是在内部查询中使用单引号,但它们不必转义查询属性,因为没有。它引发了另一个问题,因此将发布另一个so问题。