基本上,您提出的查询应该可以工作。SQLite 将日期存储为 ISO-8601 格式的字符串,该格式具有字典顺序和时间顺序相同的属性。同样使用 datetime 列TEXT
具有相同的属性。
因此,子查询的排序desc
应导致以下结果:
ID |
约会时间 |
团队 |
18 |
2021-03-02 13:30:00 |
一个 |
14 |
2021-03-02 12:30:00 |
一个 |
16 |
2021-03-02 12:00:00 |
一个 |
5 |
2021-03-02 11:30:00 |
乙 |
.. |
.. |
.. |
如另一个答案所述,group by 的问题是任意选择每个组的返回行,但看起来它始终是第一行。知道了这一点,我们有不同的解决方案来确定它应该返回哪些行:
# Using an aggregate function in select
session.query(TeamHistory.team, func.max(TeamHistory.changed_at)).group_by(TeamHistory.team).all()
# Using an aggregate function with `having`
session.query(TeamHistory).group_by(TeamHistory.team).having(func.max(TeamHistory.changed_at).all()
这将产生以下工作示例:
from sqlalchemy import Column, Integer, create_engine, Text, DateTime, func
from sqlalchemy.orm import sessionmaker, deferred, column_property
from sqlalchemy.ext.declarative import declarative_base
import datetime
Base = declarative_base()
class TeamHistory(Base):
__tablename__ = 'team_history'
id = Column(Integer, primary_key=True)
changed_at = Column(DateTime)
team = Column(Text)
if __name__ == '__main__':
engine = create_engine('sqlite://')
Base.metadata.create_all(engine)
Session = sessionmaker(engine)
db = Session()
lst = [{"ID":1,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":2,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":3,"changed_at":"2021-03-02 10:30:00","team":"B"},{"ID":4,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":5,"changed_at":"2021-03-02 11:30:00","team":"B"},{"ID":6,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":7,"changed_at":"2021-03-02 11:00:00","team":"B"},{"ID":8,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":9,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":10,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":11,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":12,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":13,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":14,"changed_at":"2021-03-02 12:30:00","team":"A"},{"ID":15,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":16,"changed_at":"2021-03-02 12:00:00","team":"A"},{"ID":17,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":18,"changed_at":"2021-03-02 13:30:00","team":"A"},{"ID":19,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":20,"changed_at":"2021-03-02 10:00:00","team":"A"}]
for dct in lst:
dt = datetime.datetime.strptime(dct.get('changed_at'), '%Y-%m-%d %H:%M:%S')
nth = TeamHistory(id=dct.get('ID'), changed_at=dt, team=dct.get('team'))
db.add(nth)
db.commit()
res = db.query(TeamHistory)\
.select_entity_from(
db.query(TeamHistory).order_by(TeamHistory.changed_at.desc()).subquery()
).group_by(TeamHistory.team)\
.all()
for r in res:
print(r.id, r.changed_at, r.team)
print()
res = db.query(TeamHistory)\
.group_by(TeamHistory.team)\
.having(func.max(TeamHistory.changed_at))\
.all()
for r in res:
print(r.id, r.changed_at, r.team)
print()
res = db.query(TeamHistory.id, func.max(TeamHistory.changed_at), TeamHistory.team)\
.group_by(TeamHistory.team)\
.all()
for r in res:
print(r[0], r[1], r[2])
如果您主要对团队的最后一次更改感兴趣,您也可以使用deferred
and column_property
to only get MAX(changed_at)
。
from sqlalchemy import Column, Integer, create_engine, Text, DateTime, func, select
from sqlalchemy.orm import sessionmaker, deferred, column_property
from sqlalchemy.ext.declarative import declarative_base
import datetime
Base = declarative_base()
class TeamHistory(Base):
__tablename__ = 'team_history_def'
id = Column(Integer, primary_key=True)
changed_at = deferred(Column(DateTime))
last_changed = column_property(func.max(changed_at))
team = Column(Text)
if __name__ == '__main__':
engine = create_engine('sqlite://')
Base.metadata.create_all(engine)
Session = sessionmaker(engine)
db = Session()
lst = [{"ID":1,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":2,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":3,"changed_at":"2021-03-02 10:30:00","team":"B"},{"ID":4,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":5,"changed_at":"2021-03-02 11:30:00","team":"B"},{"ID":6,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":7,"changed_at":"2021-03-02 11:00:00","team":"B"},{"ID":8,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":9,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":10,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":11,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":12,"changed_at":"2021-03-02 10:00:00","team":"A"},{"ID":13,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":14,"changed_at":"2021-03-02 12:30:00","team":"A"},{"ID":15,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":16,"changed_at":"2021-03-02 12:00:00","team":"A"},{"ID":17,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":18,"changed_at":"2021-03-02 13:30:00","team":"A"},{"ID":19,"changed_at":"2021-03-02 10:00:00","team":"B"},{"ID":20,"changed_at":"2021-03-02 10:00:00","team":"A"}]
for dct in lst:
dt = datetime.datetime.strptime(dct.get('changed_at'), '%Y-%m-%d %H:%M:%S')
nth = TeamHistory(id=dct.get('ID'), changed_at=dt, team=dct.get('team'))
db.add(nth)
db.commit()
res = db.query(TeamHistory)\
.group_by(TeamHistory.team)\
.all()
for r in res:
print(r.id, r.changed_at, r.team)
print()