[英]PostgreSQL 9.6 crosstab, create pivot
How in PostgreSQL 9.6, from a table "import" with structure below in the link... 在PostgreSQL 9.6中,如何从表格中的“导入”表中的链接下面...
create a query / function, then transpose it to something like this: 创建一个查询/函数,然后将其转置为以下内容:
Unfortunately, table 'import' has no ID field. 不幸的是,表“导入”没有ID字段。 I tried using crosstab with tablefunc, but no effect. 我尝试将交叉表与tablefunc一起使用,但没有效果。
You are looking for the opposite of a cross-tab or pivot: you are looking for an un-pivot 您正在寻找交叉表或枢轴的对立面:您正在寻找非枢轴
In standard SQL you would do this with a UNION ALL (unless the DBMS supports the unpivot
operator which Postgres does not): 在标准SQL中,您可以使用UNION ALL来执行此操作(除非DBMS支持Postgres不支持的unpivot
运算符):
select dlimportdate, 1 as colno, col1 as value
from the_table
union all
select dlimportdate, 2, col1
from the_table
union all
...
However in Postgres there is a shorter way to do this. 但是在Postgres中,有一种更短的方法可以做到这一点。 Create an array of the columns, then use unnest to turn them into rows: 创建一个由列组成的数组,然后使用unnest将它们变成行:
select dlimportdate, t.colno, t.value
from the_table
cross join unnest(array[col1, col2, col3, ...]) with ordinality as t(value, colno);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.