这是我的SQLAlchemy查询代码
medium_contact_id_subq = (g.session.query(distinct(func.unnest(FUContact.medium_contact_id_lis))).filter(FUContact._id.in_(contact_id_lis))).subquery()
q = (g.session.query(FUMessage).
filter(FUMessage.fu_medium_contact_id.in_(medium_contact_id_subq))
.order_by(desc(FUMessage.timestamp_utc))
)
我想将FUMessage
的{{1}}限制为medium_contact_id
N个结果。
作为一种解决方法,这是我目前丑陋且未经优化的代码:
medium_contact_id_lis = (g.session.query(distinct(func.unnest(FUContact.medium_contact_id_lis))).filter(FUContact._id.in_(contact_id_lis))).all()
q = None
for medium_contact_id_tup in medium_contact_id_lis:
medium_contact_id = medium_contact_id_tup[0]
if q is None:
q = (g.session.query(FUMessage)
.filter(FUMessage.fu_medium_contact_id == medium_contact_id)
.limit(MESSAGE_LIMIT)
)
else:
subq = (g.session.query(FUMessage)
.filter(FUMessage.fu_medium_contact_id == medium_contact_id)
.limit(MESSAGE_LIMIT)
)
q = q.union(subq)
q = q.order_by(desc(FUMessage.timestamp_utc))
答案 0 :(得分:3)
在每个组中获取热门 N 行的一种方法是在子选择中使用rank()
或row_number()
等窗口函数,并使用所需的分组和顺序,然后按在封闭选择中。对于 N = 1 ,您可以在Postgresql中使用DISTINCT ON ... ORDER BY组合。
使用函数元素的over()
方法生成窗口表达式可以直接使用SQLAlchemy:
medium_contact_id_subq = g.session.query(
func.unnest(FUContact.medium_contact_id_lis).distinct()).\
filter(FUContact._id.in_(contact_id_lis)).\
subquery()
# Perform required filtering in the subquery. Choose a suitable ordering,
# or you'll get indeterminate results.
subq = g.session.query(
FUMessage,
func.row_number().over(
partition_by=FUMessage.fu_medium_contact_id,
order_by=FUMessage.timestamp_utc).label('n')).\
filter(FUMessage.fu_medium_contact_id.in_(medium_contact_id_subq)).\
subquery()
fumessage_alias = aliased(FUMessage, subq)
# row_number() counts up from 1, so include rows with a row num
# less than or equal to limit
q = g.session.query(fumessage_alias).\
filter(subq.c.n <= MESSAGE_LIMIT)