Postgresql 在交叉表函数中使用case语句

Postgresql 在交叉表函数中使用case语句,postgresql,Postgresql,是否可以在“交叉表”函数中使用case语句?这就是我到目前为止所做的 SELECT * FROM crosstab('select distinct test_id, cluster,total_points_earned FROM pmt_cluster') case when test_id = 451 end AS Algebra( "School___Teacher" text, "Analyze functions using different repres

是否可以在“交叉表”函数中使用case语句?这就是我到目前为止所做的

SELECT * FROM crosstab('select distinct test_id,  cluster,total_points_earned FROM pmt_cluster')
case when test_id = 451 end AS Algebra( 
      "School___Teacher" text,
     "Analyze functions using different representations" text,
     "Construct and compare linear, quadratic, and exponential models and solve problems" text,
     "Create equations that describe numbers or relationships" text,
     "Expressions and Equations" text)
case when test_id = 454 end AS Ela( 
     "School___Teacher" text,
     "Key Ideas and Details" text,
     "Conventions of Standard English" text,
     "Craft and Structure" text,
     "Vocabulary Acquisition and Use" text)

如果我理解正确,您需要从psql调用它。因为用任何外部语言创建所需的SQL语句都没有问题

要执行此操作,可以使用\gset命令。它将当前查询输入缓冲区发送到服务器,并将查询的输出存储到psql变量中。()


获取以下错误:错误:您没有运行GSET查询的权限。我想如果我能通过这个权限错误,它会工作的。这是第一个gset。你可以从psql运行它吗?你可以从postgres用户那里试试吗?您使用哪个版本的postgresql?我们有两个平台:测试和生产。当我在生产环境中运行时,我得到“没有权限”错误。然而,当我从test运行它时,我得到了以下错误:错误:SQL错误:SQLSTATE[42601]:语法错误:7错误:语法错误在“\”行或附近2:\gset^Query:\gset选择CASE when:test\u id=451,然后是“代数”when:test\u id=454,然后是“Ela”结束为ttype,“School\uu\uu教师”作为f1,“text”作为t1,“text”作为tX,案例:test_id=451然后“使用不同表示分析函数”当:test_id=454然后“关键思想和细节”结束为f2,。。。。。
SELECT 451 AS test_id;
\gset


SELECT CASE WHEN :test_id = 451 THEN 'Algebra' WHEN :test_id = 454 THEN 'Ela' END AS ttype,
        '"School___Teacher"' AS f1, 'text' AS t1,
        'text' AS tX,
        CASE WHEN :test_id = 451 THEN '"Analyze functions using different representations"' WHEN :test_id = 454 THEN '"Key Ideas and Details"' END AS f2,
        CASE WHEN :test_id = 451 THEN '"Construct and compare linear, quadratic, and exponential models and solve problems"' WHEN :test_id = 454 THEN '"Conventions of Standard English"' END AS f3,
        CASE WHEN :test_id = 451 THEN '"Create equations that describe numbers or relationships"' WHEN :test_id = 454 THEN '"Craft and Structure"' END AS f4,
        CASE WHEN :test_id = 451 THEN '"Expressions and Equations"' WHEN :test_id = 454 THEN '"Vocabulary Acquisition and Use"'  END AS f5;
\gset

SELECT * 
      FROM crosstab('select distinct test_id,  cluster,total_points_earned FROM pmt_cluster')
        AS :ttype(:f1 :t1, :f2 :tX, :f3 :tX, :f4 :tX, :f5 :tX);
;