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