原表如下:
A B C D E F
zzs 0 1 0 1 6
djc 0 1 0 0 12
csjj 1 0 0 0 12
dhj 1 0 1 0 3
dh 0 0 1 1 5
想用SQL查询一个新表,当 B列为1时,F列求和记为B1;当C列为1时,F列求和记为C1,以此类推,结果如下表:
B1 C1 D1 E1
15 18 8 11
请问程序怎么写
select
sum(case when b = 1 then f else 0 end) B1,
sum(case when c = 1 then f else 0 end) C1,
sum(case when d = 1 then f else 0 end) D1,
sum(case when e = 1 then f else 0 end) E1
from
table a