gil*_*sur 7 postgresql pivot crosstab
我有一个交叉表函数,过去曾多次成功使用过,但现在它在最后转储所有数据,而不是将其转储到输出表中。似乎找不到交叉表。我对其进行了以下研究;
下面是一段函数代码: BEGIN str := '" " text,'; -- A1 单元格中的空白
FOR rec IN SELECT DISTINCT col_name FROM an_in_tbl ORDER BY col_name LOOP str := str || '"' || rec.col_name || '" 文本' ||','; 结束循环;
str:= substring(str, 0, length(str));
EXECUTE 'CREATE EXTENSION IF NOT EXISTS tablefunc;
DROP TABLE IF EXISTS an_out_tbl;
CREATE TABLE an_out_tbl AS
SELECT *
FROM crosstab(''select row_name, col_name, row_value from an_in_tbl order by 1'',
''SELECT DISTINCT col_name FROM an_in_tbl ORDER BY 1'')
AS final_result ('|| str ||')';
Run Code Online (Sandbox Code Playgroud)
选择animal_pivot_fn()
注意:扩展“tablefunc”已存在,跳过 注意:表“an_out_tbl”不存在,跳过错误:函数 crosstab(未知,未知)不存在 第 5 行:FROM crosstab('select row_name, col_name, row_value from... ^ 提示:没有函数与给定名称和参数类型匹配。您可能需要添加显式类型转换。查询:CREATE EXTENSION IF NOT EXISTS tablefunc; DROP TABLE IF EXISTS an_out_tbl; CREATE TABLE an_out_tbl AS SELECT * FROM crosstab('select row_name, col_name, row_value from an_in_tbl order by 1',
'SELECT DISTINCT col_name FROM an_in_tbl ORDER BY 1') AS Final_result (" " text,"CAT" text,"DOG" text,"SNAKE" text,"HORSE" text,"ELEPHANT “文本、“鼠标”文本、“猴子”...以及更多...这里是数据被转储并且没有创建数据透视表的地方。
Sar*_*hna 10
需要运行下面的查询
运行\dx命令。
如果结果如下所示需要运行以下查询
CREATE EXTENSION tablefunc;
Run Code Online (Sandbox Code Playgroud)
再次运行\dx命令,结果应如下所示。
现在您可以运行交叉表查询,它应该可以解决。
| 归档时间: |
|
| 查看次数: |
13273 次 |
| 最近记录: |