首页 > 解决方案 > 创建 Tablefun 扩展后使用“Crosstab”功能

问题描述

我希望使用交叉表函数在 Postgres 中旋转我的查询。我已经创建了扩展:tablefunc 但我的查询不起作用(我已经执行了内部查询并且它有效)

以下是我的查询:

    select *
    from crosstab
         (
        'select form_type, title, count(title) as  total
        from table_name
         where timestamp >= '2019-01-01'
         and title in ('this_form','that_form')
        group by form_type, title
         order by 1,2,3'
        ) 
        AS ct("Form_type" text, "Form" int, "Form_2" int)   

内部查询的结果:

    form_type  || title || total

     A            Form1     5
     B            Form2     9

我想要使​​用 CROSSTAB 返回的内容:

    form_type  || Form1 || Form2

     A            5         
     B                       9

标签: sqlpostgresqlcrosstab

解决方案


推荐阅读