【问题标题】:Using crosstab and pivot使用交叉表和数据透视表
【发布时间】:2021-01-30 05:27:58
【问题描述】:

我有一张这样的桌子-

MonthID PlanType    Count

202004  Medicare    1582
202004  Medicaid    6820
201904  Medicare    3875
201904  Other       12598
201905  Other       41817
201905  Medicare    16886
201905  Medicaid    20877

我想使用 pivot 来获得这个输出 -

Month  Medicare  Other  Medicaid 
201905 16886     41817  20877
201904 3875      12598  
202004 1582             6820

【问题讨论】:

    标签: sql postgresql pivot


    【解决方案1】:

    您可以使用过滤聚合:

    select month, 
           sum(count) filter (where plantype = 'Medicare') as medicare,
           sum(count) filter (where plantype = 'Medicaid') as medicaid,
           sum(count) filter (where plantype = 'Other') as other
    from the_table
    group by month;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多