Home
Analytics
Aggregating getDuration as a Sum - ignores minutes/fractions
rkraus10
<p>I have a report that is displaying regular hours and overtime hours in columnar format. These fields are fetched as getDuration data. The Data set identifies these fields as "string." I have a group SUM for these fields. The problem is that it only sums the integer and ignored fractions of the hour.</p>
<p> </p>
<p>For instance:</p>
<p>2:00</p>
<p>4:30</p>
<p>1:00</p>
<p>
</p>
<p>7</p>
<p> </p>
<p>How can I get the sum to work correctly?</p>
<p> </p>
<p>Thanks in advance.</p>
<p> </p>
Find more posts tagged with
Comments
micajblock
<p>What version of BIRT are you using? Also can you share how the data is being retrieved from your data source? Easier to build an actual example.</p>
Matthew L.
<p>You can split the string value and convert it into seconds for the SUM aggregation, then convert the seconds back into a time formatted string via onRender method.</p>
<p> </p>
<p>See attached example.</p>
rkraus10
<p>I am using BIRT for Maximo through Eclipse 3.7.1</p>
<p> </p>
<p>My fetch statement for the startdate is:</p>
<p>row["regularhrs"] = maximoDataSet.getDuration("regularhrs");</p>
<p> </p>
<p>My output column is defined as a String</p>
<p> </p>
<p>I have standard SQL select script to grab regularhrs which has the data type of FLOAT.</p>
micajblock
<p>Does the example above not work for you?</p>
rkraus10
<p>Matthew L - the conversion works as it converts the time to seconds, but the on Render does not run.</p>
<p>I get 36000 for 10:00.</p>
Matthew L.
<p>Attached is a modified version that hopefully will work for you.</p>
<p>All the code is now in the Aggregations Expression section.</p>
<p>The Aggregation Data Type and Function must be changed to display the String output.</p>
<p>The Aggregation settings are:</p>
<p>Data Type: String</p>
<p>Function: LAST</p>
<p> </p>
<p>Let me know if there are any other issues.</p>
rkraus10
<p>Also what I need is a decimal, not minutes, so I should get 7.5 and not 7:30.</p>
<p>It seems like it should be a real easy thing to convert.</p>
<p>Am I wrong?</p>
Matthew L.
<p>To return a value without the time:format, you would just divide out the hours and minutes from the seconds total.</p>
<p>See example </p>
rkraus10
<p>I made some changes to get decimal equivalent of the minutes. Here is the script:</p>
<p> </p>
<p>====</p>
<p> </p>
<p>var parts = row["Column1"].split(':'); //Split string<br>
if(!seconds){var seconds=0;} //If seconds isnt set<br>
seconds = parseInt(seconds) + parseInt((+parts[0]) * 60 * 60 + (+parts[1]) * 60); //Add seconds up per each loop<br>
var output = toHHMMSS(parseInt(seconds)); //Set output var (See last line of this code section)<br><br>
//From: <a data-ipb='nomediaparse' href='
http://fiddle.jshell.net/stefanz/XKBeT/1/'>http://fiddle.jshell.net/stefanz/XKBeT/1/</a><br>
;
function toHHMMSS(sec) {<br>
var sec_num = parseInt(sec, 10); // don't forget the second parm<br>
var hours = Math.floor(sec_num / 3600);<br>
var minutes = Math.floor((sec_num - (hours * 3600)) / 36); //THIS -> converts the minutes into a fraction of an hour<br>
var seconds = sec_num - (hours * 3600) - (minutes * 60);<br><br>
//if (hours < 10) {hours = "0"+hours;} //Leading 0<br>
if (minutes < 10) {minutes = "0"+minutes;} //Leading 0<br>
if (seconds < 10) {seconds = "0"+seconds;} //Leading 0<br>
var time = hours+'.'+minutes;//+':'+seconds; //Added a dot instead of a colon.<br>
return time;<br>
}<br><br>
output; //Return value</p>
<p> </p>
<p>=======</p>
<p> </p>
<p>I will try it in my Maximo Birt report and see if it flies</p>
Matthew L.
<p>You could also just drop the function and use this code instead:</p>
<p> </p>
<p>var parts = row["Column1"].split(':'); //Split string<br>
if(!seconds){var seconds=0;} //If seconds isnt set<br>
seconds = parseInt(seconds) + parseInt((+parts[0]) * 60 * 60 + (+parts[1]) * 60); //Add seconds up per each loop<br>
var output = (parseInt(seconds) / 60) / 60;<br>
output; //Return value</p>
rkraus10
<p>It works!</p>
<p>I was using this aggregate as a group total, so I needed to zero out the hours, minutes and seconds at the top of the script:</p>
<p> </p>
<p>hours=0;<br>
minutes=0;<br>
seconds=0;</p>
<p> </p>
<p>thanks!!!</p>