1👍
✅
You can use DISTINCT ON date_trunc('day', datetime)
in the query to return only one record per day from the database:
SimLogs
|> where(number: ^sim_number)
|> distinct([s], fragment("date_trunc('day', ?)", s.datetime))
|> order_by(asc: :datetime)
|> Repo.all
To fetch the last record per day, you can to modify the order_by
:
|> order_by([s], [asc: fragment("date_trunc('day', ?)", s.datetime), desc: s.datetime])
Source:stackexchange.com