简体   繁体   中英

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.

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

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.

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.

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
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM