>select *; > from person p > inner join referral r on p.cl_ref = r.cl_ref and !empty(r.re_date); > where gomonth(p.cl_dob, 216) > (select min(rr.re_date) from referral rr; > where rr.cl_ref = p.cl_ref and !empty(rr.re_date)) >>
SELECT P.*,r.*; FROM; FORCE (select cl_ref , min(re_date) re_date from referral rr WHERE re_date<>{} GROUP BY 1) X; JOIN person p ON p.cl_ref=x.cl_ref AND gomonth(p.cl_dob, 216) > x.re_date; JOIN referral r on r.cl_ref = p.cl_ref ; WHERE {} <> r.re_date * or SELECT P.*,r.*; FROM; FORCE (select cl_ref , min(re_date) re_date from referral rr WHERE re_date<>{} GROUP BY 1) X; JOIN person p ON p.cl_ref=x.cl_ref AND gomonth(p.cl_dob, 216) > x.re_date; JOIN referral r on r.cl_ref = p.cl_ref ; HAVING r.re_date <> {}