OrE*_*lse 1 sql t-sql select sum count
来自我的自定义视图的输出如下......
Col1 Col2 Col3 Col4
xxxx Cake 1 1*
Cake xxxx 2* 1
xxxx Cake 2 0*
xxxx Cake 0 0*
Cake xxxx 2* 0
Cake xxxx 2* 0
Run Code Online (Sandbox Code Playgroud)
我想要总结的是......
For every row,
if the word Cake is found in Col1, then add the value of Col3 to Sum
else add the value of Col4 to sum
Run Code Online (Sandbox Code Playgroud)
从上面的视图得到的SUM应该是1 + 2 + 0 + 0 + 2 + 2 = 7
提前致谢!
ps添加星号只是为了显示,应将哪些数字添加到总和.
Fra*_*k V 12
有点像
select
SUM(
Case
when Col1 = 'Cake' then Col3
when Col2 = 'Cake' then Col4
else 0 END
) as MySum
from TheView
Run Code Online (Sandbox Code Playgroud)