0

我遇到了一个我无法解决的问题。我有一个包含以下列的表格: Personid, type.

每个可以有几行personid 我想要的结果将是每个人有多少行类型 5,12,71 的聚合。

所以表格将如下所示:

  Personid  type 5  type 12  type 71

   11          0       2        7
   15          1       6        0
4

1 回答 1

2
select
  personid,
  count(case when type = 5 then 1 end) type5cnt,
  count(case when type = 12 then 12 end) type12cnt,
  count(case when type = 71 then 71 end) type71cnt
from table_name
group by personid
于 2012-12-19T08:28:45.170 回答