tags:

views:

137

answers:

5

select max(DELIVERY_TIMESTAMP) from DOCUMENTS; will return the time that the latest document was delivered. How do I return the other columns for the latest document? For example I want DOC_NAME for the document that was most recently delivered?

I'm not sure how to form the WHERE clause.

+1  A: 

Select Max(DELIVERY_TIMESTAMP), Doc_Name From TableName Group By Doc_Name

That should do it, unless I missed something in the question.

Jeremy Reagan
I don't know of any SQL variant where this is guaranteed to work, and at least on Oracle it certainly won't.
Alnitak
This works only if you order BY MAX(DELIVERY_TIMESTAMP) and take TOP 1.
Cade Roux
+5  A: 
SELECT
  DELIVERY_TIMESTAMP,
  OTHER_COLUMN
FROM
  DOCUMENTS
WHERE
  DELIVERY_TIMESTAMP = (SELECT MAX(DELIVERY_TIMESTAMP) FROM DOCUMENTS)
Tomalak
This is better then mine, I vote for yours.
Jeremy Reagan
+4  A: 

You have a few options

SELECT DOC_NAME
FROM DOCUMENTS
WHERE DELIVERY_TIMESTAMP IN (
    SELECT MAX(DELIVERY_TIMESTAMP)
    FROM DOCUMENTS
)

Or with joins

SELECT DOC_NAME
FROM DOCUMENTS
INNER JOIN (
    SELECT MAX(DELIVERY_TIMESTAMP) AS MAX_DELIVERY_TIMESTAMP
    FROM DOCUMENTS
) AS M
    ON M.MAX_DELIVERY_TIMESTAMP = DOCUMENTS.DELIVERY_TIMESTAMP

It gets more complicated if there are duplicates in a timestamp or you need multiple columns in your "max" criteria (because MAX() is only over the one column for all rows)

This is where the JOIN option is the only option available, because a construction like this is not available (say multiple orders with identical timestamp):

SELECT DOC_NAME
FROM DOCUMENTS
WHERE (DELIVERY_TIMESTAMP, ORDERID) IN (
    SELECT TOP 1 DELIVERY_TIMESTAMP, ORDERID
    FROM DOCUMENTS
    ORDER BY DELIVERY_TIMESTAMP DESC, ORDERID DESC
)

Where you in fact, would need to do:

SELECT DOC_NAME
FROM DOCUMENTS
INNER JOIN (
    SELECT TOP 1 DELIVERY_TIMESTAMP, ORDERID
    FROM DOCUMENTS
    ORDER BY DELIVERY_TIMESTAMP DESC, ORDERID DESC
) AS M
    ON M.DELIVERY_TIMESTAMP = DOCUMENTS.DELIVERY_TIMESTAMP
        AND M.ORDERID = DOCUMENTS.ORDERID
Cade Roux
Cade, I was going to reply with the JOIN solution, and then I inspected the execution plan for each technique. Sql Server 2005 (the db I have handy) uses the same execution plan for each syntax.So Paul, you have your choice of either... and performance will not suffer
Jeff Fritz
Yes, the optimizer in SQL Server is very good. I will modify my answer to note that the JOIN solution is the only one which works when you are dealing with a MAX tuple.
Cade Roux
Thank you very much!
Paul Croarkin
+2  A: 

In MSSQL, the following works too:

SELECT TOP 1 * FROM DOCUMENTS ORDER BY DELIVERY_TIMESTAMP DESC
TrevorD
+2  A: 

On some versions of SQL (i.e. MySQL) you can do this:

SELECT *
FROM DOCUMENTS
ORDER BY DELIVERY_TIMESTAMP DESC
LIMIT 1
Alnitak
This returns all the documents. I only want the most recent document.
Paul Croarkin
@Paul: The query has the "LIMIT" clause. It will never return more than one record. The returned record is the one with the max value for 'DELIVERY_TIMESTAMP' (the recent document that you want)
Vijay Dev