婆罗门
精华
|
战斗力 鹅
|
回帖 0
注册时间 2013-1-1
|
本帖最后由 alvin051414 于 2019-3-11 20:25 编辑
--a表数据可靠的话
--标准sql
select
a.id,
a.name,
case when sub_a.max_value is null then 0 else sub_a.max_value end as highest,
case when sub_b.max_value is null then 0 else sub_b.max_value end as highestFullTime
from a
left join
(select b.aid,max(c.value) as max_value from b left join c on b.level=c.level group by b.aid) sub_a
on a.id=sub_a.aid
left join
(select b_full.aid,max(c.value) as max_value from (select * from b where type='全日制') b_full
left join c on b_full.level=c.level group by b_full.aid) sub_b
on a.id=sub_b.aid
--Oracle SQL之类的可以写的更漂亮
select
a.id,
a.name,
nvl(max(c.value),0) as highest,
max(decode(b.type,'全日制',c.value,0)) as highestFullTime
from a
left join b on a.id=b.aid
left join c on b.level=c.level
group by a.id,a.name
--ab表缺数据的话就刺激了,要outer join再加一堆case when或者nvl2了
--s1这代码模式不兼容case when的么… |
|