bobg
bobg

Reputation: 23

SQL SELECT statement MAX date and table joins

In MySQL, I have a two tables as below :

         ClientTable               
    clientID    clientName            
    1           Client A              
    2           Client B             
    3           Client C             
    4           Client D             
    5           Client E              
    6           Client F              



                 NotesTable
    noteID  clientID    note    noteDate
    1           3       Test 1    12-Jun-14
    2           3       Test 2    18-Aug-14
    3           4       Test 3    23-Oct-14
    4           6       Test 4    25-May-14
    5           3       Test 5    25-Nov-14
    6           6       Test 6    16-Jul-14

I want to select all the clients from the client table and, where a note exists for the client, the date of the latest note entry. If no note exists for a client, then return null for the noteDate. Desired result set as follows :

    client ID   clientName  latestNoteDate  
    1           Client A    null    
    2           Client B    null    
    3           Client C    25-Nov-14   
    4           Client D    23-Oct-14   
    5           Client E    null    
    6           Client F    16-Jul-14   

Any help appreciated, I have tried a few options using nested Select with MAX(noteDate) and various left joins but can't seem to get it right.

Upvotes: 1

Views: 476

Answers (5)

Abercrombieande
Abercrombieande

Reputation: 699

Why all the subqueries?

select ct.clientID, ct.clientName,max(nt.noteDate) latestNoteDate
from ClientTable  ct
left outer join NotesTable nt
on ct.clientID = nt.clientID 
group by ct.clientID, ct.clientName

Upvotes: 2

Danielle S
Danielle S

Reputation: 11

select clienttable.clientID, clienttable.clientName, notes.noteDate
left outer join NotesTable notes on notes.clientID = clienttable.clientID and noteDate = (select      max(noteDate) from NotesTable where notes.clientID = clienttable.clientID)

It will return null if there are no note entries.

OR

select clienttable, clientID, clienttable.clientName, (select max(noteDate) from NotesTable where notes.clientID = clienttable.clientID) noteDate

Upvotes: 0

Robert Bain
Robert Bain

Reputation: 9576

Try the following code, which uses a correlated sub-query.

SELECT ct.clientID,
       ct.clientName,
       (SELECT MAX(noteDate)
        FROM   notesTable nt
        WHERE  nt.clientID = ct.clientId)
FROM   clientTable ct

Upvotes: 0

eemikula
eemikula

Reputation: 93

Looks like a good place for using a sub-query. Try something like:

select c.id, c.name, n.latestNoteDate from client c
left join
(select clientid, MAX(notedate) as latestNoteDate from note
group by clientid) as n on n.clientid = c.id

The key is, find the data you want from the notes table first, then use that to join with the client data later.

Upvotes: 0

sgeddes
sgeddes

Reputation: 62831

You can use an outer join with a subquery:

select c.clientid, c.clientname, n.latestnotedate
from client c
   left join (
       select clientId, max(noteDate) latestnotedate
       from notes
       group by clientId
   ) n on c.clientId = n.clientId

This assumes the max(noteDate) is the latest note entry. If that's not the case, easy enough to use the noteid instead and then just include one additional join.

Upvotes: 1

Related Questions