简体   繁体   English

SQLAlchemy:选择行,按日期列分组,其日期时间列对于每个日期都是最新的

[英]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.给定 PostgreSQL 数据库中的下表。

value价值 created_at created_at date日期
2 2 2022-02-01 01:01:01 2022-02-01 01:01:01 2022-03-15 2022-03-15
10 10 2022-02-15 01:01:01 2022-02-15 01:01:01 2022-03-15 2022-03-15
5 5 2022-02-02 01:01:01 2022-02-02 01:01:01 2022-03-16 2022-03-16
6 6 2022-02-16 01:01:01 2022-02-16 01:01:01 2022-03-16 2022-03-16

How can you write sqlalchemy code which:您如何编写 sqlalchemy 代码:

  1. Groups by the date columndate列分组
  2. Get's the most recent created_at value for each date获取每个日期最近的created_at
  3. Returns all rows with meet that criteria返回满足该条件的所有行

The output of above query should be the following:上述查询的输出应如下所示:

value价值 created_at created_at date日期
10 10 2022-02-15 01:01:01 2022-02-15 01:01:01 2022-03-15 2022-03-15
6 6 2022-02-16 01:01:01 2022-02-16 01:01:01 2022-03-16 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但是得到错误"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.而根据我的理解,使用聚合函数或在 GROUP BY 子句中包含“值”不会提供所需的结果。

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.我在我正在做的项目中遇到了同样的问题,但是在阅读了有关relationshipsdistinct文档之后,我提出了这个查询。 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
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按每个唯一列值的最近日期过滤 Pandas 数据框 - Filter Pandas dataframe by most recent date for each unique column value SqlAlchemy:将datetime列转换为date列 - SqlAlchemy: Convert datetime column to date column 在 dataframe 列中查找第二个最近的日期 - Find second most recent date in a dataframe column 按日期过滤 sqlalchemy sqlite 日期时间列 - Filter sqlalchemy sqlite datetime column by date 按日期时间列中的日期过滤 flask / SQLalchemy 中的帖子 - filtering posts in flask / SQLalchemy by a date in a datetime column Python:在一列中查找最近的日期,而另一列中没有匹配的日期 - Python: find most recent date in one column with no matching date in another pandas dataframe函数返回日期最近的行,并且其中一列包含输入值,抛出错误 - pandas dataframe function to return rows where date is most recent and one of the column contains the input value, throwing error 使用groupby选择最新数据,想要附加一列以返回数据的日期 - Used groupby to select most recent data, want to append a column that returns the date of the data SQLAlchemy:获取具有最新日期的对象 - SQLAlchemy: get the object with the most recent date Pandas DataFrame - 如何在按另一列分组时获取每列的最新值 - Pandas DataFrame - How to get most recent value for each column when grouped by another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM