如何查询联接列上具有唯一值的行?

2024-10-04 05:27:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试让我的常用查询子查询删除重复地点.id,但它不会删除它。这是下面的代码。我尝试过使用distinct,但它不遵循规则的顺序。在

SimilarPost = aliased(Post)
SimilarPostOption = aliased(PostOption)
popular_query = (db.session.query(Post, func.count(SimilarPost.id)).
         join(Place, Place.id == Post.place_id).
         join(PostOption, PostOption.post_id == Post.id).
         outerjoin(SimilarPostOption, PostOption.val == SimilarPostOption.val).
         join(SimilarPost,SimilarPost.id == SimilarPostOption.post_id).
         filter(Place.id == Post.place_id).
         filter(self.radius_cond()).
         group_by(Post.id).
         group_by(Place.id).
         order_by(desc(func.count(SimilarPost.id))).
         order_by(desc(Post.timestamp))
         ).subquery().select()

all_posts = db.session.query(Post).select_from(filter.pick()).all()

我用

^{pr2}$

我该怎么解决这个问题?在

谢谢!在


Tags: iddbbysessioncountplacefilterquery
2条回答

这会让你得到你想要的:

SimilarPost = aliased(Post)
SimilarPostOption = aliased(PostOption)
post_popularity = (db.session.query(func.count(SimilarPost.id))
        .select_from(PostOption)
        .filter(PostOption.post_id == Post.id)
        .correlate(Post)
        .outerjoin(SimilarPostOption, PostOption.val == SimilarPostOption.val)
        .join(SimilarPost, sql.and_(
                SimilarPost.id == SimilarPostOption.post_id,
                SimilarPost.place_id == Post.place_id)
        )
        .as_scalar())
popular_post_id = (db.session.query(Post.id)
        .filter(Post.place_id == Place.id)
        .correlate(Place)
        .order_by(post_popularity.desc())
        .limit(1)
        .as_scalar())

deduped_posts = (db.session.query(Post, post_popularity)
        .join(Place)
        .filter(Post.id == popular_post_id)
        .order_by(post_popularity.desc(), Post.timestamp.desc())
        .all())

我不能谈论大型数据集的运行时性能,可能有更好的解决方案,但这正是我从许多源代码(MySQL JOIN with LIMIT 1 on joined tableSQLAlchemy - subquery in a WHERE clauseSQLAlchemy Query documentation)中合成的。最大的复杂因素是您显然需要使用as_scalar将子查询嵌套在正确的位置,因此无法从同一个子查询中同时返回Post id和count。在

FWIW,这是一个庞然大物,我同意user1675804,SQLAlchemy代码这么深很难摸索,而且不太容易维护。您应该仔细研究更多的低技术解决方案,如向数据库添加列或用python代码做更多的工作。在

我不想听起来像个坏人但是。。。在我看来,你对这个问题的处理方式似乎远没有最佳。。。如果你使用的是postgresql,你可以简化整个过程。。。但是,考虑到我假设这些帖子的阅读频率要比更新的频繁得多,一个更好的方法是将一些列添加到表中,这些列在插入/更新时由触发器更新到其他表中,至少如果性能可能会成为一个问题,这是我将采用的解决方案

对sqlalchemy不太熟悉,所以不能用清晰的代码编写它,但是我能想到的另一个解决方案至少使用一个子查询为groupby中的每个列选择order by中的内容,这将大大增加您已经很慢的查询

相关问题 更多 >