How to get last records for last 24 hours from postgres using slick.

13 views
Skip to first unread message

Shreyas Tandale

unread,
Dec 28, 2018, 1:52:34 AM12/28/18
to Slick / ScalaQuery
Hi,
   Apologies if this seems like a silly question. I am a newbie to both scala and slick.
   I have a table in postgres with name "users" containing a column called "created_on" whose datatype is timestamp. I am using slick in scala to query this table. I want to get all users who were created in the last 24 hours. Using postgres, I was able to get it done. The query i wrote was - select user_id from users where created_on > NOW() - INTERVAL '24 hour';
   Can anyone tell me how this condition can be written using slick in scala? 

Ivano Pagano

unread,
Dec 28, 2018, 2:53:35 AM12/28/18
to Slick / ScalaQuery
If you don't specifically need to keep the logic in the sql query itself, you may simply use Scala to compute the threshold value for the timestamp (using java.time._ for example).

After you have a threshold as a scala value, the query should become as simple as 

usersQuery.map(_.userIf).filter(_.createdOn > threshold)

I hope this gives you enough material do figure out details on your own

Cheers
Ivano

Shreyas Tandale

unread,
Dec 28, 2018, 3:38:28 AM12/28/18
to Slick / ScalaQuery
Hey Ivano. Thank you for your prompt reply. Let me try this approach.
Reply all
Reply to author
Forward
0 new messages