我构造了以下JPQL查询,其中根据传入的时间戳发送结果。 我希望对此进行修改,并传递一个数字,而不是月份值,例如,以获取过去6个月的所有结果。
如果相关,我正在使用Oracle DB。我尝试在此查询中传递datediff方法,但它引发编译错误。 有没有办法做到这一点?
我当前的查询。
@Query("SELECT SUM(urlCount.referEventTotalCount) " +
"FROM UrlCountEntity urlCount " +
"JOIN urlCount.eventTypes et " +
"WHERE urlCount.url.urlTx IN :urls " +
"AND et.referEventTypeDs =:eventType " +
"AND urlCount.updateTimeStamp <=:timeStamp")
Integer getTotalCount(@Param("urls") List<String> urls, @Param("eventType")String eventType, @Param("timeStamp")Timestamp timeStamp);
要修改该行->“ AND urlCount.updateTimeStamp <=:timeStamp”)
@Query("SELECT SUM(urlCount.referEventTotalCount) " +
"FROM UrlCountEntity urlCount " +
"JOIN urlCount.eventTypes et " +
"WHERE urlCount.url.urlTx IN :urls " +
"AND et.referEventTypeDs =:eventType ")
// "AND urlCount.updateTimeStamp <=:timeStamp") // instead of this, pass in the numOfMths value to calculate how much data to show (example past 3 mths)
Integer getTotalCount(@Param("urls") List<String> urls, @Param("eventType")String eventType, @Param("numOfMths")int numOfMths);
答案 0 :(得分:1)
对于Oracle,您可以使用带有负值的ADD_MONTH减去。
ADD_MONTHS(SYSDATE, -6)
您可以这样调用此函数:
function('ADD_MONTH', CURRENT_DATE, -6)
因此您的查询可能如下所示:
@Query("SELECT SUM(urlCount.referEventTotalCount) " +
"FROM UrlCountEntity urlCount " +
"JOIN urlCount.eventTypes et " +
"WHERE urlCount.url.urlTx IN :urls " +
"AND et.referEventTypeDs =:eventType " +
"AND urlCount.updateTimeStamp >= function('ADD_MONTH', CURRENT_DATE, :month)")