-1

How can I get the latest date in my query without duplicates and by the latest date only?

 SELECT 
  MEMB.LAST_M,
  MEMB.MEMB_N,
  PrintDate
FROM
  MEMB 
  INNER JOIN tblPrint 
    ON MEMB.MEMB_N = tblPrint.MEMB_N 
 WHERE tblPrint.`PrintDate` IN (SELECT tblPrint.`PrintDate` FROM tblPrint)

My above query gives me this:

img

As you can see in the image it includes two "Andres, Jose" one with 2020 date and the other one with 2024 date and one "Loria Marisa" but I just need 1 Andres Jose with 2024 date.

It includes the same value but I don't need it.

Community
  • 1
  • 1
Vin
  • 169
  • 2
  • 16
  • 1
    have you try using select distinct and order by desc – Kelvin Jul 29 '19 at 09:07
  • 1
    This is a FAQ, so no need to go further but, for next time, see: [Why should I provide an MCVE for what seems to me to be a very simple SQL query?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) And welcome to SO – Strawberry Jul 29 '19 at 09:12
  • @Kelvin I tried it but didn't work, the answer by darkRob work for me – Vin Jul 29 '19 at 09:16

1 Answers1

1

You need group by and max. Group by for your columns and max for your date, since you want to select max date only.

SELECT 
  MEMB.LAST_M,
  MEMB.MEMB_N,
  MAX(PrintDate)
FROM
  MEMB 
  INNER JOIN tblPrint 
    ON MEMB.MEMB_N = tblPrint.MEMB_N 
 WHERE tblPrint.`PrintDate` IN (SELECT tblPrint.`PrintDate` FROM tblPrint)
 GROUP BY MEMB.LAST_M,
  MEMB.MEMB_N
DarkRob
  • 3,843
  • 1
  • 10
  • 27