日期:2014-05-18  浏览次数:20467 次

求一SQL语句,在线等,会的很简单,送分的,大家帮帮忙
有如下表
id             condition
1                         2
2                         3
3                         6
4                         9
要求统计个数,当condition为奇数时算1,为偶数时酸2,最后得到一个count(*)的结果,比如上表经SQL语句后得到结果集为2+1+2+1=6,请问该select语句该如何写,高手帮忙啊不过通过符合还是简单查询,最后直接通过数据库引擎获得一个结果集就可以

------解决方案--------------------
declare @tb table(id int, condition int)
insert @tb select 1 ,2
union all select 2 , 3
union all select 3 , 6
union all select 4 , 9

select sum(case when condition%2=0 then 2 else 1 end)
from @tb

/*
6
*/
------解决方案--------------------
select sum(case when condition % 2 =0 then 2 else 1 end)
from 表名

------解决方案--------------------
都写对了,不重复了,接分