-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Help: HQL query to select first records in time interval.
PostPosted: Fri Sep 05, 2008 3:38 am 
Newbie

Joined: Fri Sep 05, 2008 2:41 am
Posts: 1
Location: South Africa
Hi

I have a situation where devices in the field send their readings and is persisted every hour. The devices measure consumption with a counter (cumulative). This counter reading is persisted every hour.
The persistant entity is as follows:

Code:
class Reading {
int deviceId;
int reading;
Date timestamp;
}


I need to report on the total consumption of all the devices during a time interval (say, a day), and how many devices' reading i used.
To get an accurate report, for each device, the difference between the first reading (minimum reading) of the current time interval and the next time interval must be calculated.

I wrote a query which gives me the first reading (smallest reading ) for each device for a time interval. But now I need to get te second reading as well, which will be the smallest reading after the nextIntervalStart time. I don't know if a subquery is the answer?

Current query:
Code:
select r.deviceId, min(r.reading),
from Reading r
where r.timestamp >= :intervalStart
and r.timestamp < :nextIntervalStart
group by r.deviceId


It would be nice to have an HQL query which returns the device id, and 2 readings (the first reading of each interval per device).

Query result wanted:
Code:
deviceId, reading1, reading2
1, 2000, 2100
2, 1000, 1150
3, 1560, 1800


Or a query which returns device id and consumption delta.
Code:
deviceId, consumption
1, 100
2, 150
3, 240


Thanks, any help will be appreciated.

_________________
- Marais


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.