Hibernate mysql group按天连续结果

时间:2012-10-23 10:58:34

标签: java mysql sql hibernate

我有这个功能

我需要在sql(hibernate或mysql)或者跨越结果数组的java函数中这样做

我需要在白天获得一个可靠的结果

从propCode.propClass中选择DAY(prop.docCreationDate),count(prop.docfullName)作为prop,其中prop.docCreationDate> ='“+ startDate +”00:00:00'和prop.docCreationDate< =' “+ endDate +”23:59:59'GROUP BY DAY(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  

1 个答案:

答案 0 :(得分:2)

我假设您的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进行测试。