سؤال

I've created the following query:

select k.vname, k.nname, coalesce(
  to_char((
    select a1.ablaufdat
    from kunde k1
    natural join abonnement a1
    where k.vname = k1.vname and k.nname = k1.nname
    and a1.ablaufdat < to_date('01-01-2015', 'DD-MM-YYYY')
))
, '-')
from kunde k
left outer join abonnement a on k.knr = a.knr 
order by k.vname asc, k.nname asc;

It shows me all customers with a subscription which ends before 2015 with the corresponding date or - if there is no corresponding date, which is the intended result.

However, the query is too complicated in my opinion and I couldn't find a simplier way. I would appreciate if you could help me with simplifying it.
Thank you very much.

هل كانت مفيدة؟

المحلول

You say that there can be just one abonnement per kunde. So just outer join and see if you got a record:

select k.knr, k.vname, k.nname,
  coalesce(to_char(a.ablaufdat), '-' ) as ablaufdatum
from kunde k 
left outer join abonnement a on a.knr = k.knr and a.ablaufdat < to_date('01-01-2015', 'DD-MM-YYYY')
order by k.vname asc, k.nname asc;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top