示例
with
tab1 as
(select
case
when (count(su.insu_pkg_id)>0) then 2
else 0
end cnt
from
ecs_a_bene_insu_pkg su,ecs_a_bene_pkg pk
where
su.pkg_id=pk.pkg_id and pk.pkg_type=5012 and su.insu_id=367801 and su.status=1
),
tab2 as
(select
case
when (count(su.insu_pkg_id)>0) then 2
else 0
end cnt
from
ecs_a_bene_insu_pkg su,ecs_a_bene_pkg pk
where
su.pkg_id=pk.pkg_id and pk.pkg_type=5011 and su.insu_id=367801 and su.status=1
)
select tab1.cnt+tab2.cnt cnt from tab1,tab2
示例
with
tab1 as
(select
case
when (count(su.insu_pkg_id)>0) then 2
else 0
end cnt
from
ecs_a_bene_insu_pkg su,ecs_a_bene_pkg pk
where
su.pkg_id=pk.pkg_id and pk.pkg_type=5012 and su.insu_id=367801 and su.status=1
),
tab2 as
(select
case
when (count(su.insu_pkg_id)>0) then 2
else 0
end cnt
from
ecs_a_bene_insu_pkg su,ecs_a_bene_pkg pk
where
su.pkg_id=pk.pkg_id and pk.pkg_type=5011 and su.insu_id=367801 and su.status=1
)
select tab1.cnt+tab2.cnt cnt from tab1,tab2
本文通过两个具体的SQL案例展示了如何使用CASE WHEN语句结合子查询来计算特定条件下的计数总和。通过对不同表的连接和筛选,实现了根据不同pkg_type对insu_pkg_id的存在情况进行计数,并最终将两个临时表的计数结果相加。
2314

被折叠的 条评论
为什么被折叠?



