多个leftjoin重复计数

2014-11-24 15:45:41 · 作者: · 浏览: 2

各位大神,今天见到一个sql,如下,查询t1.id有在与t2.id进行左联结时的条数,发现运用了大量重复的左联结,感觉很不好,但又不知道怎么修改这个sql,请指教:

create table t1(id number);
create table t2(id number,val varchar2(10));

select a1.id,count(1)
  from t1 a1
  left outer join t2 b1
    on a1.id = b1.id
   and b1.val = 'a'
   left outer join t2 b2
   on a1.id=b2.id
   and b2.val='b'
   left outer join t2 b2
   on a1.id=b2.id
   and b2.val='c'
   left outer join t2 b2
   on a1.id=b2.id
   and b2.val='d'
   left outer join t2 b2
   on a1.id=b2.id
   and b2.val='e'
group by a1.id;