总和(情况......结束)分组

时间:2015-03-12 12:46:09

标签: sql sql-server

select sum(case when(remark='Regular') then count(*)
               when(remark='Half Day') then count(*)* 0.5
           end),
       shift
from attendance
where empid='1447' and mont='02' and payyear='2015'
group by shift, remark
order by shift

如何在SQL Server中编写?

1 个答案:

答案 0 :(得分:2)

将案例放在聚合函数中:

select sum(case when remark = 'Regular' then 1
                when remark = 'Half Day' then 0.5
           end),
       shift
from attendance
where empid='1447' and mont='02' and payyear='2015'
group by shift
order by shift

另外,从remark删除group by