需求:如果t_grade>1,则对t_id累加操作
创建表
create table #a_aa_a(
t_id int,
t_name varchar(10),
t_date int,
t_grade int
)
insert into #a_aa_a VALUES (132154,'盲僧',20210401,1)
insert into #a_aa_a VALUES (231654,'盲僧',20210403,2)
insert into #a_aa_a VALUES (236587,'盲僧',20210405,3)
insert into #a_aa_a VALUES (365214,'亚索',20210401,1)
insert into #a_aa_a VALUES (365214,'亚索',20210403,2)
insert into #a_aa_a VALUES (325412,'亚索',20210405,3)
insert into #a_aa_a VALUES (365584,'盖伦',20210401,1)
insert into #a_aa_a VALUES (236587,'盖伦',20210403,2)
insert into #a_aa_a VALUES (326985,'盖伦',20210408,3)
得到结果:报错信息如下
语句改进如下:增加group by 分组语句
select case when t_grade >1 then sum(t_id) else 0 end
from #a_aa_a group by t_grade
结果:
验证结果是否正确:
修改语句后计算对t_grade计算的影响