在JpaRepository中使用postgres函数时间戳

时间:2018-12-23 10:28:42

标签: java spring postgresql

我尝试仅按日期分组,active_to列是时间戳记,因此它也有时间。该查询可在pgAdmin中使用,但JpaRepository似乎有问题,即使它是本机查询也是如此。如何修改此查询以使用JpaRepository?

@Query(value = "SELECT o.active_to::timestamp::date, count(o) as sum from work_order o group by o.active_to::timestamp::date order by o.active_to::timestamp::date  asc limit 7", nativeQuery = true)

我收到此错误:

org.postgresql.util.PSQLException: ERROR: syntax error at or near ":"
  Position: 19

1 个答案:

答案 0 :(得分:0)

您不能使用:,因为这是开始命名参数的字符。

您必须使用演员表。

@Query(value = "SELECT cast(cast(o.active_to as timestamp) as date), count(o) as sum " +
               "from work_order o group by cast(cast(o.active_to as timestamp) as date) " +  
               "order by cast(cast(o.active_to as timestamp) as date) asc limit 7",
               nativeQuery = true)

Cast和::类似。在此处了解更多信息:

https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS