0

In my Rails project, I have a scores table full of timestamp rows that I need to query and find the LATEST record from each day. My production database is PostgreSQL though I develop/test locally in sqlite3.

I'm having trouble structuring the SQL query to pull just the date from the timestamp. So if this is the timestamp: 2010-12-13 23:03:40.485012+0000, then I need to just examine 2010-12-13 and ignore the time.

Scores table:

id          :integer  
name        :string  
score       :integer  
updated_at  :timestamp  

And since I have multiple rows per day for each name, I'd like to find the MAX timestamp if each day, and just look at the latest score of each day for each name.

I've tried using the DATE() function without any luck. Anyone know the best method of doing this?

0

3 Answers 3

1

In the the SQLite date functions, you need a colon to separate hours and minutes in the timezone.

sqlite> select date('2010-12-13 23:03:40.485012+0000');
[NULL]
sqlite> select date('2010-12-13 23:03:40.485012+00:00');
2010-12-13

My production database is PostgreSQL though I develop/test locally in sqlite3.

This is a bad idea. Test with the same database you use for production.

Sign up to request clarification or add additional context in comments.

Comments

0

For PostgreSQL you can use:

CAST(updated_at AS DATE)

2 Comments

Thanks, trying to get something locally that will work with SQLite3 first. Not sure why "SELECT DATE(updated_at) from Scores" isn't working.
According to the SQLLite manual DATE() converts a string to a DATE, it does not extract the date from a timestamp. A bit off-topic but I would never recommend to test an application with a different DBMS than what is used in production.
0

In PostgreSQL you can use ::.

declare _now timestamp with time zont := now();
declare dt date := _now::date;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.