I am dealing with a problem where I need to aggregate data based on a criteria. The situation is a follow:
I have consumption data from an application on database and I need to show it to be displayed in a table. People can aggregate data based on minute, hour, day and month. When people aggregate data by minute, for example, data will be display in a table to show at max 10 rows (but here is the tricky part, the rows are hour based in this example). So instead of saying I want the first 10 rows (of the minutes aggregation) I have to find what is the number of entries equivalent to display the results of 10 distinct hours.
I have tried to make the query and I am at this point to better illustrate the problem (query won't run):
SELECT count(*) as COUNT,
year([CONSUMPTION_DATE]) as YEAR_BY_MINUTE,
month([CONSUMPTION_DATE]) as MONTH_BY_MINUTE,
day([CONSUMPTION_DATE]) as DAY_BY_MINUTE,
DATEPART ( hour , [CONSUMPTION_DATE] ) as HOUR_BY_MINUTE,
DATEPART ( minute , [CONSUMPTION_DATE]) as MINUTE_BY_MINUTE
FROM [CONSUMPTION_TABLE]
where (YEAR_BY_MINUTE, MONTH_BY_MINUTE, DAY_BY_MINUTE, HOUR_BY_MINUTE) IN
(select top 10 year([CONSUMPTION_DATE]) as YEAR_BY_HOUR,
month([CONSUMPTION_DATE])as MONTH_BY_HOUR,
day([CONSUMPTION_DATE]) as DAY_BY_HOUR,
DATEPART ( hour , [CONSUMPTION_DATE] ) as HOUR_BY_HOUR
FROM [CONSUMPTION_TABLE]
where [CONSUMPTION_DATE] between '2018-01-18' and '2018-03-20'
GROUP BY year([CONSUMPTION_DATE]),
month([CONSUMPTION_DATE]),
day([CONSUMPTION_DATE]),
DATEPART ( hour , [CONSUMPTION_DATE])
)
GROUP BY year([CONSUMPTION_DATE]),
month([CONSUMPTION_DATE]),
day([CONSUMPTION_DATE]),
DATEPART ( hour , [CONSUMPTION_DATE] ),
DATEPART ( minute , [CONSUMPTION_DATE] )
I also have been trying to go by another way but query is running with error thought:
;with MinuteGroup as
(
select year([CONSUMPTION_DATE]) as YEAR_GROUP_MINUTE,
month([CONSUMPTION_DATE])as MONTH_GROUP_MINUTE,
day([CONSUMPTION_DATE]) as DAY_GROUP_MINUTE,
DATEPART ( hour , [CONSUMPTION_DATE] ) as HOUR_GROUP_MINUTE,
DATEPART ( minute , [CONSUMPTION_DATE] ) as MINUTE_GROUP_MINUTE
FROM [CONSUMPTION_TABLE]
where [CONSUMPTION_DATE] between '2018-01-18' and '2018-03-20'
group by year([CONSUMPTION_DATE]),
month([CONSUMPTION_DATE]),
day([CONSUMPTION_DATE]),
DATEPART ( hour , [CONSUMPTION_DATE] ),
DATEPART ( minute , [CONSUMPTION_DATE] )
)
select * from MinuteGroup where
(YEAR_GROUP_MINUTE, MONTH_GROUP_MINUTE, DAY_GROUP_MINUTE, HOUR_GROUP_MINUTE)
in ( select year([CONSUMPTION_DATE]),
month([CONSUMPTION_DATE]),
day([CONSUMPTION_DATE]) ,
DATEPART ( hour , [CONSUMPTION_DATE] )
FROM [CONSUMPTION_TABLE]
where [CONSUMPTION_DATE] between '2018-01-18' and '2018-03-20'
group by year([CONSUMPTION_DATE]),
month([CONSUMPTION_DATE]),
day([CONSUMPTION_DATE]),
DATEPART ( hour , [CONSUMPTION_DATE])
)
Is another way easier to make this or what do I have to change in the queries to make them work?
PS: I will have to transfer this to HQL so if it will be easier I will also accept that answers.
EDIT:
As is hard to visualize what I am trying to achieve here is an image :
(the unit time I am aggregating will be a simple number of 1,2,3,4,5,...)