【问题标题】:Range COUNT query Based on DATE from epoch timestamp in Hibernate范围 COUNT 查询基于 Hibernate 中纪元时间戳的 DATE
【发布时间】:2014-06-20 22:59:06
【问题描述】:

我在 postgres 中有一个由纪元时间戳索引的表....现在我想查询此表以获取特定日期之间的事件数...我已经在 postgres 中对其进行了查询,但我我不确定如何在 hibernate 中实现这一点,而无需在 hibernate 中编写本机 postgres 查询......

我的表结构是

event
-----------------
id,      created,     updated,    event_type,     source_machine         timestamp
1       07.05.2011                  event1                machine1        938970822
2       07.05.2011                  event2                machine2       1332949476
3       07.05.2011             NullPointerException       machine2        648034359
4       06.05.2011                  event2                machine2       1173340611
5       06.05.2011                  event1                machine1       1391797768
6       05.05.2011                  event2                machine1       849970844
7       04.05.2011                  event1                machine2        
*Currently, table has 10k rows.

仅供参考:我不能为此使用 CREATED 列,我只能使用时间戳... 到目前为止,我在 postgres 中写的查询是......

select event_type,to_char(to_timestamp(timestamp),'YYYY-MM-DD') AS date_range,count(*) from event where to_char(to_timestamp(timestamp),'YYYY-MM-DD') between to_char(to_timestamp(549788400),'YYYY-MM-DD') and to_char(to_timestamp(1403247600),'YYYY-MM-DD') and event.machine = 'machine1' group by event_type,to_char(to_timestamp(timestamp),'YYYY-MM-DD');

如果我不使用它,我必须使用“to_char”。 《2010-03-31 23:59:59》。边界条件具有潜在危险:如果系统在 23:59:59 和 00:00:00 之间有交易,我会错过它。任何帮助将不胜感激。

【问题讨论】:

    标签: java hibernate postgresql orm range-query


    【解决方案1】:

    我做了一个支持休眠的查询。如果有人正在寻找类似问题的答案,这是我的解决方案。 上面的 HQL 查询。

    SELECT new map(E.event_type as name,to_char(to_timestamp(E.timestamp),'MM-DD-YYYY') AS key,count(*) as value) from Event E WHERE E.timestamp BETWEEN " + startTime + " and " + endTime + " AND E.platform = '"+platform+"' AND E.event_type = '"+eventName+"' GROUP BY E.event_type,to_char(to_timestamp(E.timestamp),'MM-DD-YYYY') ORDER BY to_char(to_timestamp(E.timestamp),'MM-DD-YYYY')";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 2016-10-09
      • 2017-04-02
      • 2013-10-22
      • 2020-05-05
      相关资源
      最近更新 更多