python - SQLAlchemy:两个日期之间的差异
问题描述
我想知道在 SQLAlchemy 上工作的两个日期之间的区别。使用 Postgresql 我有以下工作:
SELECT
EXTRACT(EPOCH FROM ('2019-02-11 17:59:05.953894'::timestamp - '2019-02-11 17:59:01.953894'::timestamp))
但是,在 SQLAlchemy 中尝试相同时遇到问题:
session.query(func.extract('epoch',func.date(subquery.c.dt_final.cast(Date)))-
func.date(subquery.c.dt_start.cast(Date))).all()
收到此错误:
ProgrammingError: (psycopg2.errors.UndefinedFunction) operator does not exist: double precision - date
LINE 1: ...h FROM date(CAST(anon_2.dt_final AS DATE))) - date(CAS...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
在 SQLAlchemy 中获取两个日期之间差异的正确方法是什么?
谢谢
解决方案
您可能需要从时间戳切换到日期时间,但是有一个名为 timediff() 的内置函数,它的工作原理类似于 count() 和 max()。我不熟悉 SQLalchemy,但它应该很普遍。
推荐阅读
- xamarin - 删除 XamarinForms 中的 ActionBar 顶部标题
- python - CNN 架构中的神经元数量
- pandas - Pandas - numpy 和多个条件
- angular - 断言错误:此 TNode 不属于此 TView - Angular 从 v8 更新到 v10 之后
- r - 与 R Postgres 后端中唯一/不同的类似聚合函数
- django - 问题将 id 传递到 url 尝试在 Django 中获取
- angular - 如何减少对象分配中的重复代码
- reactjs - 使用 React 17.0.01 的后端 Spring Boot RESTful Web 服务不起作用 tomcat 8.4.25
- azure-cognitive-search - 如何停止 zh-Hans.microsoft 分析器匹配几乎任何东西
- r - 带有 IRkernels 的 jupyter notebooks 中的 Plot 中的希腊字母