HQL select sum and time group by time unit (hour/day/month) and

ε祈祈猫儿з 提交于 2020-01-04 05:20:35

问题


I have a table like this:

COLUMN               TYPE
------------------------------
ID                   INTEGER
VALUE                INTEGER
TIME                 TIMESTAMP

How can I write query with HQL selecting sum of value column groupped by time unit(f.e. groupped by day) and selecting this time unit as a second column.

I have tried to do it and got something like this: but there is no parsedatetime function in HQL so I have no idea how can I get the proper query now.

select sum(value),
parsedatetime(day(time) || '.' || month(time) || '.' || year(time) || ' ' || hour(time) ||':00:00', 'dd.MM.yy hh:mm:ss')
as xtime 
from Table 
group by time

I want this query to return object with 2 fields: int and java.sql.Date.


回答1:


You are grouping by whole the datetime value, that's why you haven't expected result, you must group it by the piece of datetime that you want, try something like below:

select sum(value),
day(time) as xtime 
from Table 
group by day(time)


来源:https://stackoverflow.com/questions/33710305/hql-select-sum-and-time-group-by-time-unit-hour-day-month-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!