【问题标题】:Hibernate mysql groupe by day concequetive resultsHibernate mysql 按天分组连续结果
【发布时间】:2012-10-23 10:58:33
【问题描述】:

我有这个功能

我需要在 sql(hibernate 或 mysql)或影响结果数组的 java 函数中执行此操作

我需要一个按天分组的结果

从 propCode.propClass 中选择 DAY(prop.docCreationDate), count(prop.docfullName) 作为 prop where prop.docCreationDate >= '" + startDate + " 00:00:00' and prop.docCreationDate

i have this entitie count in my table
2012-10-05   3
2012-10-06   0
2012-10-07   7
2012-10-08   13
2012-10-09   9
2012-10-10   0
2012-10-11   0
2012-10-12   3

the request return me this values
5    3
7    7
8    13
9    9
12   3

in this way i loose three lignes that have 0 as value, i need a request that return me this
5    3
6    0
7    7
8    13
9    9
10   0
11   0
12   3  

【问题讨论】:

    标签: java mysql sql hibernate


    【解决方案1】:

    我假设在您的propClass 表中,您每天有多个带有文档路径名称的条目。由于您提供的条目已经几乎是您需要的,除了一天的转换,它与使用的查询不匹配。

    考虑到这一点,您提到的条目是每天的文档总数,我假设您在缺失的日子里插入了 0 个值。如果是这样,您可以使用time_intervals 临时表,就像提到的here 一样。然后您应该能够执行以下操作:

    call interval_between('"+ startDate +"', '" + endDate + "', 'DAY', 1);
    
    select day(ti.interval_from), count(prop.docfullName) from time_intervals ti left join propClass as prop 
    on prop.docCreationDate>=ti.interval_from AND prop.docCreationDate < ti.interval_from + INTERVAL 1 DAY
    where ti.interval_from>='"+ startDate +"' and ti.interval_from < '" + endDate + "'
    group by ti.interval_from;
    

    使用native queries你可以从hibernate调用上面两个语句。

    SqlFiddle测试。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    相关资源
    最近更新 更多