首页 > 解决方案 > 如何在sql中获取列值作为表列

问题描述

这是我在数据库中定义的表:

pi_value    | Status
------------+-------------------
    500.000 | Bank Submitted
   500.0000 | Bank Submitted
  1000.0000 | Maturity Received
  4000.0000 | Bank Submitted
    50.0000 | Maturity Received

我希望输出看起来像这样:

Maturity Received | Bank Submitted | 
------------------+----------------+
       1050.0000  |    5000.0000   |
------------------+----------------+

标签: sqlpostgresqlpivotcrosstab

解决方案


使用条件聚合

  select sum(case when status='Bank Submitted' then pi_value else 0 end),
  sum(case when status='Maturity Received' then pi_value else 0 end) from table

推荐阅读