views:

221

answers:

3

Hello all,

I have a Journal_Entry table, with a Primary Key of Journal_Entry_ID, and (among other columns) an Entry_Date column.

I'm trying to do a query that selects the most recent Entry_Date -- via a SELECT MAX(Entry_Date) -- but the problem is that the user may have logged more than one entry on a given date. So if the user logged a journal entry twice today, this SELECT statement could return more than one row because the same MAX Entry_Date has been logged more than once.

So what I'd like to do is, if the SELECT MAX statement returns more than one record, choose the record that has the highest Journal_Entry_ID of the ones returned.

Right now my query looks like this:

SELECT Journal_Entry_ID, Entry_Date
FROM Journal_Entry
WHERE Entry_Date = (SELECT MAX(Entry_Date) FROM Journal_Entry);

I'm using SQL SERVER. Any help would be greatly appreciated.

Thanks.

EDIT: I'm using SQL SERVER. Not My SQL as I had originally reported.

A: 
SELECT TOP 1 Journal_Entry_ID, Entry_Date
FROM Journal_Entry
WHERE Entry_Date = (SELECT MAX(Entry_Date) FROM Journal_Entry)
ORDER BY Journal_Entry_ID DESC;
Pentium10
@Pentium10, Thanks for the answer. But I think it will blow up because the SELECT MAX query cannot return more than one record. If it does, Entry_Date = [multiple records] will not work. Your query above still doesn't achieve what I need, I don't think.
Mega Matt
try out the query, the `SELECT MAX` returns the date 1 record, and you filter the records by that date eg: 10 records for that day, further the records are ordered descending by entry id, so the recent entry is on position 1, reading further you missed the `TOP 1` clause that will choose only 1 record... so that will be the most recent entry for the entry date
Pentium10
+1  A: 
SELECT TOP 1
       Journal_Entry_ID, Entry_Date
FROM Journal_Entry
ORDER BY Entry_Date DESC, Journal_Entry_ID DESC

Note that the WHERE clause is not necessary here since we are ordering by Entry_Date already.

Kyle Butt
Correct answer, but why is it community wiki?
Alex Bagnolini
@Pentium10: there is no `WHERE` clause and no sub-query in this answer, that's why it's good (to me).
Alex Bagnolini
Please see my edit above. My apologies. I am using SQL Server after all. Sorry for the confusion.
Mega Matt
@kyle: can you update it with `TOP 1`? OP changed requirement from mysql to SQL Server
Alex Bagnolini
I think is slower compared to when you use WHEN statement. As the 2 order bys, needs two temp tables, and compared to a optimized select max simple sub query creating two temp tables takes longer
Pentium10
+4  A: 

Assuming Entry_Date is a Date/Time, wouldn't something like this work?

select top 1 Journal_Entry_ID
from JournalEntry
order by Journal_Entry_ID desc, Entry_Date desc

If Entry_Date is only a Date field (no time), is Journal_Entry_ID an auto-increment numeric field? If so, then maybe this:

select top 1 Journal_Entry_ID
from  JournalEntry
where Entry_Date = (select max(Entry_Date) from JournalEntry)
order by Journal_Entry_ID desc
DWong
The first would not - there's no order on `journal_entry_id` to ensure it's the highest
OMG Ponies
You are correct. I was typing too fast! I revised with the additional Order By clause.
DWong