简体   繁体   中英

SQLAlchemy: Select rows, grouped by a date column, whose datetime column is most recent for each date

Given the following table in a PostgreSQL database.

value created_at date
2 2022-02-01 01:01:01 2022-03-15
10 2022-02-15 01:01:01 2022-03-15
5 2022-02-02 01:01:01 2022-03-16
6 2022-02-16 01:01:01 2022-03-16

How can you write sqlalchemy code which:

  1. Groups by the date column
  2. Get's the most recent created_at value for each date
  3. Returns all rows with meet that criteria

The output of above query should be the following:

value created_at date
10 2022-02-15 01:01:01 2022-03-15
6 2022-02-16 01:01:01 2022-03-16

I have tried the following query:

query = (
            select(
                func.max(TABLE.created_at),
                TABLE.date,
                TABLE.value
            )
            .group_by(models.TABLE.date)
        )

But get the error "value" must appear in the GROUP BY clause or be used in an aggregate function

While, based on my understanding, using an aggregate function or including "value" in the GROUP BY clause will not provide the desired result.

What query can I use to get desired rows?

I was looking for the distinct clause

query = select(TABLE).distinct(
            TABLE.date
        ).order_by(
            TABLE.date,
            TABLE.created_at.desc(),
        )

I had the same problem at the project that I am doing, but after reading documentation about relationships and distinct i made this query. You can also try like this, hope it helps.

query = (
         select value, created_at, date, count(distinct(date)) from TABLE group by value, created_at, date
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM