mysql - How to return the most recent record of a GROUP BY statement -


I have a table (call_history) that has several records against a single ID in another ID (call_detail) Trying to return which will give me a line for each ID, but the most recent entry

For example, group all the rows against a single ID, but return only the most recent row to the updated_at field (which is a date field).

My query so far ...

  select MAX (cd.id) id, cd.first_name, cd as .summary, cd.due_at, ch. Body, ch.updated_at cd.id = ch.ticket_id as CH, call_detail from call_detail as cd.status = 'Open' and (now () () (<) Due_Eat Group BY CD.Id Hearing COUNT (*) & Gt; 1 ORDER by cd.due_at DESC   

... what i want like returns, but this gives me the oldest entry for the update_t field.

Update

My table structure is as follows:

Call_Detail id | Summary | Details | Due_at | First_name | Last_name 1 Call 1. Some text | 20/02/2014 17:00:00 | Joe Blogs 2 | Call 2 Some text | 18/02/2014 15:00:00 | Fred | Durst 3 | Call 3 Some text | 02/03/2014 01:00:00 | Joe Blogs

call_history

  id | Ticket_id | Body | Updated_at | First_name | Last_name 1 1 | Update 1 | 17/02/2014 16:00:00 | Joe Blogs 2 | 1 | Update 2 | 17/02/2014 16:02:00 | Fred | Durst 3 | 2 | Update 1 | 16/02/2014 12:02:00 | Tom | Thumb 4 | 1 | Update 3 | 17/02/2014 16:10:00 | Joe Blogs 5 | 2 | Update 2 | 17/02/2014 01:02:00 | Jack | Fraud   

etc ...

I need to recover the following output:

  ticket_id | Summary | Due_at | First_name | Body | Updated_at 1 | Call 1. 20/02/2014 17:00:00 | Joe Update 3 | 17/02/2014 16:10:00 2 calls 2 | 18/02/2014 15:00:00 | Fred | Update 2 | 17/02/2014 01:02:00    

Try this query. You need the most recent record from call_history so that you can join ( CH_MAX subquery) and then with TICKET_ID and these dates Should create a subquery with updated_at :

  cd.id id, cd.first_name, cd.summary, cd.due_at, ch.body, ch.updated_at cd left Select Call JOIN (TICKET_ID as call_detail, select Max_updated_at as updated_at by TickET_ID) as CH_MAX on cd.id = CH_MAX.ticket_id call_history as the left CH but join cd .id = ch.ticket_id And CH_MAX.max_updated_at = ch.updated_at where cd.status = 'open' and (due_at & lt; now ()) cd.due_at by DESC   

< / P> command

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -