0

我的结构看起来像

Y    | M     | P 
2018 |  8    |  A
2018 |  8    |  A
2018 |  9    |  A
2018 |  9    |  B

我正在寻求实现

Y    | M    |  A  | B
2018 |  8   |  2  | 0
2018 |  9   |  1  | 1
4

1 回答 1

1

您可以为此使用条件聚合:

select y,m,
       count(*) filter (where p = 'A') as a,
       count(*) filter (where p = 'B') as b
from the_table
group by y,m
order by y,m
于 2020-04-08T12:21:44.367 回答