Conditional sum in SQL Server -
i have table in mssql:
id op1 op2 op3 330 0 0 1 331 1 0 0 332 3 2 0
op's options. there can go 1 9, 0 means question not answer.
how can "sum" in column called "answered", so:
id op1 op2 op3 t 330 0 0 1 1 331 1 0 0 1 332 3 2 0 2
a value on 0, means answered.
i try case when, if stateme
use case
:
select id, op1, op2, op3, (case when op1 > 0 1 else 0 end + case when op2 > 0 1 else 0 end + case when op3 > 0 1 else 0 end) t mytable
Comments
Post a Comment