hooked82
hooked82

Reputation: 6376

Problem getting most recent record from SQLite DB Table

I'm having an issue getting the most recent record from my SQLite DB. I'm storing the datetime in Android using a SimpleDateFormat as follows:


Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

ContentValues values = new ContentValues();
values.put(KEY_DATE_COLUMN, dateFormat.format(now));

Viewing the values that get inserted into the DB, they are formatted as:

2011-04-07 09:00:43
2011-04-08 10:44:12
2011-04-09 05:15:10

Here's the query I've tried which gives me the first record in the table, which is the oldest record:

SELECT _id, max(datetime_column) FROM table1 GROUP By _id

Whenever I run this query, I always get the first record in that table which is 2011-04-07 09:00:43.

Can anybody see what's wrong here? Much thanks in advanced, and please let me know if any further info/code is required to help me solve this issue.

Upvotes: 1

Views: 1111

Answers (1)

Asterisk
Asterisk

Reputation: 3574

SELECT _id, datetime_column
FROM table1
WHERE datetime_column in (SELECT max(datetime_column)
                           FROM table1)

Upvotes: 4

Related Questions