我试图在Postgres中转动一张桌子。我的表'样本'有很多列(代码,部门,概念,年份,期间,价值,预订),而不是每行有1个概念,1年,1个值,我希望概念的价值符合年份。
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,而不是硬编码年份?