I will use the table structure at This page
class Book(Base):
__tablename__ = "books"
id = Column(Integer,Sequence('book_seq'),primary_key=True)
name = Column(String(50))
time_added = Column(DateTime, default=datetime.datetime.now)
Say, I have these book added to the table:
- day1, 1:00
- day1, 2:00
- day2, 1:00
- day2, 2:00
- day2, 3:00
How can I make a query that filter out the 1 and 3 rows which records the first book added at that day?
I have tried
Book.query.order_by(Book.date_added).\
group_by(db.func.date(Book.date_added, "start of day"))
But it actually gives the last result of every day, which is wrong. And now I have no idea what I can do because the document and example of sqlalchemy are so limited.
Please help me to find out what the problem is or tell me the right query statement, thanks a lot.
Edit:
Thanks for the suggestions of clarifying.
The database backend I used is sqlite
.
And for inputs and outputs of the problem:
The table books
contains many books added on different time of different days (distinct time_added
).
And I want to filter the books that were the first ones being inserted in that days. Let the days be the groups, and select the book having earliest add time in the group.