我需要写一个case语句将返回Case语句值
“1 of 3” if someone voted in one of three elections,
“2 of 3” if someone voted in two of three elections,
“3 of 3” if someone voted in three of three elections,
的问题是,某些值是一个varchar,有些是零,我不能把它们加起来。 这是我的想法,但我无法让它工作。
select
id,
CASE
WHEN race1 + race2 + race3 = 0 then '0-3'
WHEN race1 + race2 + race3 = 1 then '1-3'
WHEN race1 + race2 + race3 = 2 then '2-3'
WHEN race1 + race2 + race3 = 3 then '3-3'
WHEN race1 + race2 + race3 is null then 'Unknown'
END AS General_Turnout
from test4
http://sqlfiddle.com/#!3/cac66/3
是的我试过IsNull(race1,0),所有和删除空值并用0代替它们,但我仍然无法添加其余的。谢谢 – user973671 2012-04-11 02:47:21
你可以使用cast进行转换。我想这个例子已经在上面给出了 – 2012-04-11 02:52:11