user1543495
user1543495

Reputation: 15

MySql Complex query filtering by dates

I'm trying to see if its possible to do a specific query in mysql without having to resort to application based filtering and multiple queries.

Basically, I have a customers table, and I have a certificates table for every person ( including those which are not my customers)

Each customer can have multiple certificates, and I have the expiration date on each certificate.

I want to be able to select those customers whose certificate is about to expire (in 2 weeks or less) AND don't have another certificate with a longer expiration date.

The first part is easy, I perform an inner join based on ssn between the two tables, with the where clause specifing the date termination date from now to now + 2 weeks like so:

 select certs.ssn, certs.cert_num, certs.cert_start, certs.cert_finish 
   from certs 
  INNER JOIN customers  ON certs.ssn = customers.ssn 
  where certs.cert_finish < Date_ADD(now(), INTERVAL 14 DAY) 
   and certs.cert_finish > now()

However, I can't find a way to exclude those customers which have other certificates with a longer expiry date.

Do you guys have any ideas? I'm thinking a subquery based on max(cert_finish) on only those ssns which the previous query returned.

Upvotes: 0

Views: 255

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

You can do this with a GROUP BY and HAVING clause:

select customers.ssn, certs.cert_num, certs.cert_start, certs.cert_finish
from customers INNER JOIN
     certs
     ON certs.ssn = customers.ssn
group by customers.ssn
having sum(case when certs.cert_finish < Date_ADD(now(), INTERVAL 14 DAY) and 
                     certs.cert_finish > now()
                then 1 else 0
           end) > 0 and  -- has soon to expire certicate
       sum(case when certs.cert_finish > Date_ADD(now(), INTERVAL 14 DAY)
                then 1 else 0
           end) = 0      -- has no certificate that will expire later

If you want additional customer information, use this as a subquery and join back to the customer table.

Upvotes: 1

Related Questions