Convert rank and partition query to SqlAlchemy

前端 未结 1 1530
轮回少年
轮回少年 2021-01-04 01:00

I would like to convert the following query to SqlAlchemy, but the documentation isn\'t very helpful:

select * from (
select *, 
RANK() OVER (PARTITION BY id         


        
1条回答
  •  迷失自我
    2021-01-04 01:31

    use over expression

    from sqlalchemy import func
    
    subquery = db.session.query(
        table1,
        func.rank().over(
            order_by=table1.c.date.desc(),
            partition_by=table1.c.id
        ).label('rnk')
    ).subquery()
    
    query = db.session.query(subquery).filter(
        subquery.c.rnk==1
    )
    

    0 讨论(0)
提交回复
热议问题