Morgan Azhari
Morgan Azhari

Reputation: 209

mysql select latest date with multiple results

I have one table named DETAILS with details:

CODE | NAME    | ENTRY-DATE | NEXT-ENTRY-DATE
A001 | Adam    | 2010-01-01 | 2010-05-01
B001 | Bob     | 2010-11-01 | 2012-02-11
C001 | Charlie | 2010-01-01 | 2010-02-01
D001 | Dexton  | 2010-01-01 | 2013-12-11
A001 | Adam    | 2010-05-01 | 2013-05-15
B001 | Bob     | 2012-02-11 | 2013-02-11

what I want the results is:

CODE | NAME    | ENTRY-DATE | NEXT-ENTRY-DATE
C001 | Charlie | 2010-01-01 | 2010-02-01
D001 | Dexton  | 2010-01-01 | 2013-12-11
A001 | Adam    | 2010-05-01 | 2013-05-15
B001 | Bob     | 2012-02-11 | 2013-02-11

What I want to take is the latest entry for each code. How can I do it?

Upvotes: 0

Views: 79

Answers (2)

ooo
ooo

Reputation: 1627

Using max() will ensure that the value you get is right:

SELECT CODE, NAME, MAX(ENTRY-DATE), NEXT-ENTRY-DATE
FROM DETAILS
GROUP BY CODE;

Upvotes: 1

Mohan
Mohan

Reputation: 153

SELECT * FROM details GROUP BY code;

Upvotes: 2

Related Questions