其他sql格式也在更新中,可直接查看这个系列,要是没有你需要的格式,可在评论或私信我
exists 和 select count(1)的案例
大概sql的意思:子查询中查出满足条件的有多少个,返回一个数字,exists就是判断这个子查询里面是否返回值,如果有值就case 1 出去,没有就填入0
oracle 原sql
select
(case
when exists (select count(1)
from B lf, C la
where la.riskcode = a.riskcode
and la.ccno = lf.ccno
and lf.payname like '%嘤嘤嘤%') then
'1'
else
'0'
end) as Po
from
A a
hive 改sql
select
(case
when lfla.cout>=1 then
'1'
else
'0'
end) as Po
from
A a
,
(select count(1) as cout
from B lf, C la, A a
where la.riskcode = a.riskcode
and la.ccno = lf.ccno
and lf.payname like '%嘤嘤嘤%'
) lfla
自我理解:count(1)需要单独拿出来计算好在写在上面,用左连接需要分组,这个sql没有分组的条件,只能用逗号隔开变成一个新的表用来返回count(1)字段的值,exists改为了>=1,>=1就是有值的意思,意思是一样的