У меня три таблицы vt_owner, vt_permit, vt_tax
. Общие столбцы: regn_no, off_Cd
и state_Cd
.
Я хочу, чтобы в результате были все поля столбцов vt_owner, vt_permit
и vt_tax
, условие: (max(vt_tax.tax_upto) > max(permit.vt_permit.valid_upto) and max(permit.vt_permit.valid_upto) < '3-Oct-2019'
SELECT
vt_owner.state_cd, vt_owner.off_cd, vt_owner.regn_no, vt_owner.regn_dt,
vt_owner.regn_upto, vt_owner.purchase_dt, vt_owner.owner_name,
vt_owner.f_name, vt_owner.sale_amt, vt_owner.vh_class,
vt_owner.maker_model, vt_owner.vch_catg, vt_owner.seat_cap,
vt_owner.stand_cap, vt_owner.sleeper_cap,vt_permit.pmt_no,
vt_permit.issue_dt, vt_permit.valid_from, vt_permit.valid_upto,
vt_permit.pmt_type, vt_permit.pmt_catg, vt_permit.goods_to_carry,
vt_tax.tax_mode, vt_tax.rcpt_no, vt_tax.rcpt_dt, vt_tax.tax_from,
vt_tax.tax_upto, vt_tax.tax_amt, vt_tax.tax_fine
FROM vt_owner
JOIN permit.vt_permit ON vt_owner.regn_no = permit.vt_permit.regn_no
AND vt_owner.state_cd = permit.vt_permit.state_cd
AND vt_owner.off_cd = permit.vt_permit.off_cd
JOIN vt_tax ON vt_owner.regn_no = vt_tax.regn_no
AND vt_owner.state_cd = vt_tax.state_cd
AND vt_owner.regn_no = vt_tax.regn_no
WHERE vt_owner.state_cd='JH'
and (vt_owner.garage_add = 'NULL' OR vt_owner.garage_add = 'NA')
AND (vt_owner.status = 'Y' OR vt_owner.status = 'A')
and max(vt_tax.tax_upto) > max(permit.vt_permit.valid_upto)
and max(permit.vt_permit.valid_upto) < '3-Oct-2019'