TimeStamp的JPQL avg函数(JPQL avg function for TimeStamp)

编程入门 行业动态 更新时间:2024-10-23 08:32:09
TimeStamp的JPQL avg函数(JPQL avg function for TimeStamp)

我需要一个JPQL查询,它应该计算两个日期之间的平均天数。 我写了这样的查询

select avg(ps.begins-ps.ends) from PackStatus ps where ps.status.code = 'serviceExec' and ps.gossrvcDocPackage.serviceId = 'Usl_3641042_RSOC' and ps.begins between :startDate and :endDate and ps.ends between :startDate and :endDate

但系统抛出异常“org.hibernate.exception.DataException:无法执行查询”如果我放了一些其他字段,例如avg(ps.id),它可以正常工作。 在数据库中,ps.ends和ps.begins存储为timestapm。 我如何计算两个日期之间的平均天数? 谢谢。

I need a JPQL query which should count average days between two dates. I wrote the query like this

select avg(ps.begins-ps.ends) from PackStatus ps where ps.status.code = 'serviceExec' and ps.gossrvcDocPackage.serviceId = 'Usl_3641042_RSOC' and ps.begins between :startDate and :endDate and ps.ends between :startDate and :endDate

But the system throws exception "org.hibernate.exception.DataException: could not execute query" If I put some other field, e.g avg(ps.id) it works fine. In database ps.ends and ps.begins are stored as timestapm. How can I count avg days between two dates? Thanks.

最满意答案

找到日期之间的平均差异的正确方法是

select avg(day(ps.ends)-day(ps.begins)) from PackStatus ps where ps.status.code = 'serviceExec' and ps.gossrvcDocPackage.serviceId = 'Usl_3641042_RSOC' and ps.begins between :startDate and :endDate and ps.ends between :startDate and :endDate

你也可以找到小时,分钟等的差异。只需在小时()或其他措施上替换day()。

The right way to find avg difference between dates in days is

select avg(day(ps.ends)-day(ps.begins)) from PackStatus ps where ps.status.code = 'serviceExec' and ps.gossrvcDocPackage.serviceId = 'Usl_3641042_RSOC' and ps.begins between :startDate and :endDate and ps.ends between :startDate and :endDate

Also you can find difference in hours,minutes, etc. Just replace day() on hour() or other measure.

更多推荐

本文发布于:2023-07-29 20:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   JPQL   TimeStamp   function   avg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!