https://www.52pojie.cn/thread-1910686-1-1.html统计完成4个项目学习的学生名单casewhen嵌套初步应用,原语句少了'缺跑步、游泳、足球' 的情况,修正如下:
SELECT *, CASE when z."4" not like '%跑步%' then -- 缺跑步
case when z."4" not like '%游泳%' and z."4" not like '%篮球%' and z."4" not like '%足球%' then '缺跑步、游泳、篮球、足球'
when z."4" not like '%篮球%' and z."4" not like '%游泳%' then '缺跑步、游泳、篮球'
when z."4" not like '%篮球%' and z."4" not like '%足球%' then '缺跑步、篮球、足球'
when z."4" not like '%游泳%' and z."4" not like '%足球%' then '缺跑步、游泳、足球' --原语句缺失新增加
when z."4" not like '%游泳%' then '缺跑步、游泳'
when z."4" not like '%足球%' then '缺跑步、足球'
when z."4" not like '%篮球%' then '缺跑步、篮球'
else '缺跑步' end
when z."4"not like '%游泳%' then
--有跑步,缺游泳
case when z."4" not like '%足球%' and z."4" not like '%篮球%' then '缺游泳、篮球、足球'
when z."4" not like '%篮球%' then '缺游泳、篮球'
else '缺游泳' end
when z."4" not like '%篮球%' then
--有跑步,有游泳,缺篮球
case when z."4" not like '%足球%' and z."4" not like '%篮球%' then '缺篮球、足球'
else '缺篮球' end
when z."4" not like '%足球%' then '缺足球'
ELSE '已完成' END '备注'
FROM zhaojf04 z
另外,针对这个统计,我想了另外一种写法,补充如下:
--统计完成跑步、游泳、篮球、足球四项的完成情况
SELECT *, case when z."4" not like '%跑步%' and z."4" not like '%游泳%' and z."4" not like '%篮球%' and z."4" not like '%足球%' then '缺跑步、游泳、篮球、足球'
when z."4" not like '%跑步%' and z."4" not like '%篮球%' and z."4" not like '%游泳%' then '缺跑步、游泳、篮球'
when z."4" not like '%跑步%' and z."4" not like '%篮球%' and z."4" not like '%足球%' then '缺跑步、篮球、足球'
when z."4" not like '%跑步%' and z."4" not like '%游泳%' and z."4" not like '%足球%' then '缺跑步、游泳、足球'
when z."4" not like '%游泳%' and z."4" not like '%足球%' and z."4" not like '%篮球%' then '缺游泳、篮球、足球'
when z."4" not like '%跑步%' and z."4" not like '%游泳%' then '缺跑步、游泳'
when z."4" not like '%跑步%' and z."4" not like '%足球%' then '缺跑步、足球'
when z."4" not like '%跑步%' and z."4" not like '%篮球%' then '缺跑步、篮球'
when z."4" not like '%游泳%' and z."4" not like '%篮球%' then '缺游泳、篮球'
when z."4" not like '%游泳%' and z."4" not like '%足球%' then '缺游泳、足球'
when z."4" not like '%足球%' and z."4" not like '%篮球%' then '缺篮球、足球'
when z."4" not like '%跑步%' then '缺跑步'
when z."4" not like '%游泳%' then '缺游泳'
when z."4" not like '%篮球%' then '缺篮球'
when z."4" not like '%足球%' then '缺足球'
ELSE '已完成' END '备注'
FROM zhaojf04 z
问题是目前实际是要统计完成跑步、游泳、篮球、足球、排球5项的完成情况,这个用枚举法好像太难写了,容易出现遗漏的情况,这次发帖主要想咨询下群里老师这个这个问题有没有好的解决方法,取长补短,感谢 |