gpt4 book ai didi

python - group_by 语句、sqlalchemy 和 postgresql 的问题

转载 作者:行者123 更新时间:2023-11-28 16:43:57 28 4
gpt4 key购买 nike

我对以下查询有疑问;这本身工作正常,但它按秒分组,我想截断秒并按分钟分组。我已经尝试过 date_trunc、extract 等,但我没有任何运气。当引擎是 sqlite 时,extract('minute') 工作正常,但不适用于 postgresql。

谁能给我指出正确的方向?

PostgreSQL 版本:x86_64-redhat-linux-gnu 上的 PostgreSQL 8.1.23

Column('id', Integer, primary_key=True),
Column('date', TIMESTAMP),
Column('src', String),
Column('dst', String),
Column('len', String),
Column('sport', String),
Column('dport', String),
Column('method', String),
Column('host', String),
Column('useragent', String),
Column('statusline', String),
Column('location', String),
Column('server', String),
Column('load', String),

now = datetime.datetime.now()
DD = now - datetime.timedelta(minutes=60)
DD = DD.strftime('%Y-%m-%d %H:%M:%S')
query = session.query(HTTP.date,HTTP.statusline, func.count(HTTP.statusline).
label('count')).filter(HTTP.statusline.like('%'+status+'%'), HTTP.date>=(DD)).group_by(HTTP.date, HTTP.statusline).order_by(asc(HTTP.date)).all()

最佳答案

在您做任何之前,请考虑升级到当前版本的 PostgreSQL 8.1 is long dead and forgotten .

不完全确定符号,但有来自 @Audrius in the comments 的更新它应该像这样工作:

query = session.query(
date_trunc('min', http.date).label('date_minute')
,http.statusline
,func.count(http.statusline).label('count')
).filter(http.statusline.contains(status)
,http.date>=(DD)
).group_by('date_minute'
,http.statusline
).order_by(asc('date_minute')).all()

基本上,在 SELECT 中使用 date_trunc('min', http.date) 而不是 http.date ,在 中使用别名>GROUP BYORDER BY

顺便说一句:我发现使用 date 作为 timestamp 的名称非常具有误导性。除此之外,我的建议是永远不要使用任何基本类型名称作为标识符。导致非常困惑的错误消息和其他难以调试的错误。

关于python - group_by 语句、sqlalchemy 和 postgresql 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866273/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com