如下表格,想实现这个功能,怎么写SQL语句。 姓名 提升 预警 其他 张三 1 0 1 ……

2025-03-22 23:15:39
推荐回答(2个)
回答1:

create table tablea_2 (xuhao int,mingc varchar(20),name1 varchar(20))
insert tablea_2 select 1,'技能提升','张三'
。。。。。。

select name1, max(case mingc when '%提升%' then 1 else 0 end ) 提升,
max(case mingc when '%预警%' then 1 else 0 end ) 预警,
max(case mingc when '%其他%' then 1 else 0 end ) 其他
into #a
from tablea_2
group by name1

update t1 set t1.提升=1 from #a t1,tablea_2 t2 where t1.name1=t2.name1 and t2.mingc like '%提升%'
update t1 set t1.预警=1 from #a t1,tablea_2 t2 where t1.name1=t2.name1 and t2.mingc like '%预警%'
update t1 set t1.其他=1 from #a t1,tablea_2 t2 where t1.name1=t2.name1 and t2.mingc not like '%预警%' and t2.mingc not like '%提升%'

回答2:

select 姓名
,sum(case 名称 when 技能提升 then 1 else 0 end) as 预警
,sum(case 名称 when 预警1 then 1 when 高级预警 then 1 else 0 end) as 预警
,sum(case 名称 when 技能保持 then 1 when 程序设计 then 1 else 0 end) as 其它
from table
group by 姓名