SQL:生成一个矩阵以显示不同值组合的计数

SQL:生成一个矩阵以显示不同值组合的计数,sql,oracle,Sql,Oracle,是否有一种解决方案可以生成下面在SQL中提出的矩阵 下表显示,100名学生参加了Cat模块的学习,其中52名学生同时参加了狗和猫的学习,但只有3名学生同时参加了猫和沙鼠的学习。等等 Modules Cat Studies Dog Studies Gerbil Studies Cat Studies 100 52 3 Dog Studies 52 75 45 G

是否有一种解决方案可以生成下面在SQL中提出的矩阵

下表显示,100名学生参加了Cat模块的学习,其中52名学生同时参加了狗和猫的学习,但只有3名学生同时参加了猫和沙鼠的学习。等等

Modules         Cat Studies  Dog Studies  Gerbil Studies
Cat Studies       100             52            3
Dog Studies       52              75            45
Gerbil Studies    3               45            60
输入数据将是

module             personCode
Cat Studies        1345
Cat Studies        1234
Gerbil Studies     5634
Dog Studies        9878
Gerbil Studies     5643
Dog Studies        7362

这个源数据库是Oracle,但我们在SQL server上有一个数据仓库,我们可以将其加载到其中

使用
分组方式进行自我
连接
,并使用
大小写
表达式进行条件计数:

select t1.module,
       sum(case when t2.module = 'Cat Studies' then 1 end) as "Cat Studies",
       sum(case when t2.module = 'Dog Studies' then 1 end) as "Dog Studies",
       sum(case when t2.module = 'Gerbil Studies' then 1 end) as "Gerbil Studies"
from tablename t1
  join tablename t2 on t1.personCode = t2.personCode
group by t1.module

请编辑您的问题并显示您正在使用的输入数据。另外,用您正在使用的数据库标记问题。