I have two tables:
article('id', 'ticket_id', 'incoming_time', 'to', 'from', 'message')
ticket('id', 'queue_id')
where tickets represent a thread of emails between support staff and customers, and articles are the individual messages that compose a thread.
I’m looking to find the article with the highest incoming time (expressed as a unix timestamp) for each ticket_id, and this is the query I’m currently using:
SELECT article.* , MAX(article.incoming_time) as maxtime
FROM ticket, article
WHERE ticket.id = article.ticket_id
AND ticket.queue_id = 1
GROUP BY article.ticket_id
For example,
:article:
id --- ticket_id --- incoming_time --- to ------- from ------- message --------
11 1 1234567 help@ client@ I need help...
12 1 1235433 client@ help@ How can we help?
13 1 1240321 help@ client@ Want food!
...
:ticket:
id --- queue_id
1 1
...
But the result looks to be the row with the smallest article id instead of what I’m looking for which is the article with the highest incoming time.
Any advice would be greatly appreciated!
This is a classic hurdle that most MySQL programmers bump into.
ticket_idthat is the argument toGROUP BY. Distinct values in this column define the groups.incoming_timethat is the argument toMAX(). The greatest value in this column over the rows in each group is returned as the value ofMAX().MAX()value occurs.The database cannot infer that you want values from the same row where the max value occurs.
Think about the following cases:
There are multiple rows where the same max value occurs. Which row should be used to show the columns of
article.*?You write a query that returns both the
MIN()and theMAX(). This is legal, but which row shouldarticle.*show?You use an aggregate function such as
AVG()orSUM(), where no row has that value. How is the database to guess which row to display?In most brands of database — as well as the SQL standard itself — you aren’t allowed to write a query like this, because of the ambiguity. You can’t include any column in the select-list that isn’t inside an aggregate function or named in the
GROUP BYclause.MySQL is more permissive. It lets you do this, and leaves it up to you to write queries without ambiguity. If you do have ambiguity, it selects values from the row that is physically first in the group (but this is up to the storage engine).
For what it’s worth, SQLite also has this behavior, but it chooses the last row in the group to resolve the ambiguity. Go figure. If the SQL standard doesn’t say what to do, it’s up to the vendor implementation.
Here’s a query that can solve your problem for you:
In other words, look for a row (
a1) for which there is no other row (a2) with the sameticket_idand a greaterincoming_time. If no greaterincoming_timeis found, the LEFT OUTER JOIN returns NULL instead of a match.