【问题标题】:HQL: group by hour(time) and sum(pcount)HQL:按小时(时间)和总和(pcount)分组
【发布时间】:2018-08-27 21:22:06
【问题描述】:

我正在使用这个查询来给我按小时分组的总和

@Query(value = "select sum(PC.count) as pcounts, hour(PC.time) as hourly from ProductCount as PC where PC.time >= :currentDate and PC.time < :nextDate group by hour(PC.time)")
List<Map> findByCountAndGroupByTime(@Param("currentDate") Timestamp currentDate, @Param("nextDate") Timestamp nextDate);

但是,我得到的日期结果是Integer。是的,在文档中它说小时(时间)会给出一个整数。

如果我希望它位于 DateTimestamp 中,我需要更改什么?

【问题讨论】:

    标签: spring hibernate spring-boot hql


    【解决方案1】:

    好吧,HOUR() 函数从传递的日期中提取小时部分(因此是整数结果),如果您需要将日期设置为 PC.time 截断为提取的小时,只需获取其他需要的部分(@ 987654323@) 并使用这些值构建一个新日历。

    【讨论】:

    • 你的意思是这样的 - stackoverflow.com/questions/33710305/… 还是我使用日历在 Java 中做到这一点?
    • 如果您只需要按小时分组,您应该保持查询不变。只需提取您需要的时间戳的其他部分,然后使用 Calendar 或 Joda DateTime 或您想要的手动构建您的小时截断时间戳。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多