如何创建一个 PostgreSQL 枢轴 table 来枢轴多列?

How to create a PostgreSQL pivot table that pivots multiple columns?

我一直在探索 PostgreSQL 的 tablefunc extension module 中的 crosstab() 函数,作为生成数据透视表的一种方式。

很棒,但似乎只适用于最基本的用例。它通常只支持三列输入:

  1. 一列值保持不变,作为行标签
  2. 转换为新列名称的一列值
  3. 一列值成为其各自的新数据透视列的值

基本上是这样的:

+------+----------+-------+
| ITEM |  STATUS  | COUNT |
+------+----------+-------+
| foo  | active   |    12 |
| foo  | inactive |    17 |
| bar  | active   |    20 |
| bar  | inactive |     4 |
+------+----------+-------+

...并产生这个:

+------+--------+--------+----------+
| ITEM | STATUS | ACTIVE | INACTIVE |
+------+--------+--------+----------+
| foo  | active |     12 |       17 |
| bar  | active |     20 |        4 |
+------+--------+--------+----------+

但是更复杂的用例呢?如果你有:

  1. 您想在输出中保持原样的多个输入列?
  2. 您想将多个输入列转换为新列?

如下例所示:

+--------+-----------------+---------+--------+-------+------------------+
| SYSTEM |  MICROSERVICE   |  MONTH  | METRIC | VALUE | CONFIDENCE_LEVEL |
+--------+-----------------+---------+--------+-------+------------------+
| batch  | batch-processor | 2019-01 | uptime |    99 |                2 |
| batch  | batch-processor | 2019-01 | lag    |    20 |                1 |
| batch  | batch-processor | 2019-02 | uptime |    97 |                2 |
| batch  | batch-processor | 2019-02 | lag    |    35 |                2 |
+--------+-----------------+---------+--------+-------+------------------+

每行的前三列应按原样保留(无分组或聚合)。 metric 列有两个关联的列(即 valueconfidence_level)作为它的支点?

+--------+-----------------+---------+--------------+-------------------+-----------+----------------+
| SYSTEM |  MICROSERVICE   |  MONTH  | UPTIME_VALUE | UPTIME_CONFIDENCE | LAG_VALUE | LAG_CONFIDENCE |
+--------+-----------------+---------+--------------+-------------------+-----------+----------------+
| batch  | batch-processor | 2019-01 |           99 |                 2 |        20 |              1 |
| batch  | batch-processor | 2019-02 |           97 |                 2 |        35 |              2 |
+--------+-----------------+---------+--------------+-------------------+-----------+----------------+

我不确定这是否仍然符合 "pivot table" 的严格定义。但是 crosstab() 或任何其他现成的 PostgreSQL 函数是否可能得到这样的结果?如果不是,那么如何使用自定义 PL/pgSQL 函数生成它?谢谢!

您可以尝试使用条件聚合

select system,MICROSERVICE , MONTH,
max(case when METRIC='uptime' then VALUE end) as uptime_value,
max(case when METRIC='uptime' then CONFIDENCE_LEVEL end) as uptime_confidence,
max(case when METRIC='lag' then VALUE end) as lag_value,
max(case when METRIC='lag' then CONFIDENCE_LEVEL end) as lag_confidence
from tablename
group by system,MICROSERVICE , MONTH

另一种方法(我使用过)是将数据写入文件,使用单独的实用程序以所需格式对其进行交叉制表,然后将结果导入新的 table。