1

reference Date: 20.10.2015

step 1: need the max date from PayTO table > max(PayTO)

step 2: check if this max(PayTO) < reference Date (20.10.2015)

MenTBL
======
TZ   |   Fname
===========
11    |  ZZZ
22    |  XXX
33    |  CCC
44    |  VVV


PayTbl
======
TZ   |  PayTO
===========
11    |  01/01/2015
11    |  03/05/2015
11    |  06/01/2016
22    |  01/01/2015
22    |  01/10/2015
33    |  01/01/2015
33    |  01/12/2015
44    |  01/01/2015
44    |  04/05/2015
44    |  18/10/2015

The result should be that way:

22    |  01/10/2015  |  XXX
44    |  18/10/2015  |  VVV

Important note: PayTO is a string Field

1
  • recommend converting PayTO to a date field as that is what it stores and far easier to use in that way Commented Oct 19, 2015 at 6:43

1 Answer 1

2

query

select p.TZ, 
date_format(max(str_to_date(p.PayTO, '%d/%m/%Y')), '%d/%m/%Y') as dt,
m.FName
from PayTbl p
inner join MenTbl m
on p.TZ = m.TZ
group by p.TZ, m.FName
having max(str_to_date(p.PayTO, '%d/%m/%Y'))
<      DATE('2015-10-20')
;

output

+----+------------+-------+
| TZ |     dt     | FName |
+----+------------+-------+
| 22 | 01/10/2015 | XXX   |
| 44 | 18/10/2015 | VVV   |
+----+------------+-------+

sqlfiddle


notes

due to the PayTO field type, have to use str_to_date. this will perform slower than the analogous query with date typed field

Sign up to request clarification or add additional context in comments.

1 Comment

no worries. only small recommendation is to develop a script to migrate your data to store it in date type ( then query simplifies even further )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.