Results 1 to 7 of 7

Thread: Running Total for Time Field

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Running Total for Time Field

    Hi Guys,

    I have a field called Total Trip Time in my table. It stores a value in the format hh:nn:ss as time. The field type is Time. How can I calculate a grand total that adds each time value in the recordset in my Crystal Report.

    Please help.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Running Total for Time Field

    Create a formula that converts the time values into seconds. Sum the formula and then convert this Grand Total into hh:mm:ss. If the Grand Total can possibly go over 24 hours then you can't use any of the normal date/time functions but convert using simple math.

  3. #3

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: Running Total for Time Field

    Quote Originally Posted by brucevde
    Create a formula that converts the time values into seconds. Sum the formula and then convert this Grand Total into hh:mm:ss. If the Grand Total can possibly go over 24 hours then you can't use any of the normal date/time functions but convert using simple math.
    Hi Brucevde,

    Thanks alot for replying. Can you please give me an example of the formula to use. I have no idea how formulas work in Crystal. I am still new to Crystal.

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Running Total for Time Field

    I have no idea how formulas work in Crystal.
    That is why they invented help files.

    Basically, a formula is executed/calculated for every record in the data set. Converting a date field to seconds you could write something like

    (Hour({TableName.FieldName}) * 3600) +
    (Minute({TableName.FieldName}) * 60) +
    Second({TableName.FieldName})

    The results of the formula can then be printed/formatted on the report just like a database field or used in other calculations, like summaries.

  5. #5

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: Running Total for Time Field

    Thanks Brucevde,

    I will try it out today.

  6. #6

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: Running Total for Time Field

    Hi Brucevde,

    Please help me out. I have a field in my db called TotTripTime stored as a Time Data type. In my crystal report query i specify that IF(t.TotTripTime = '00:00:00','',t.TotTripTime).

    Problem is, the date field is now a string in the report because of the way I have written the query as above.

    So what I tried was creating 2 different functions:

    1. One to convert the String Time to an actual time:

    Code:
    Time({Command.TotTripTime})
    2. And then another function to calculate the total based on the above functions value:

    Code:
    (Hour({@DriverHour}) * 3600) +
    (Minute({@DriverHour}) * 60) +
    Second({@DriverHour})
    But when I run my report it displays absolutely nothing. Please, please help me. It's almost working.

  7. #7

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: Running Total for Time Field

    Ok Brucvde,

    I've coem up with these 3 functions:

    1: CovertTimeStringToSeconds

    Code:
    local numbervar hours;
    local numbervar minutes;
    local numbervar seconds;
    
    // Parse out the hours portion of the string and
    // multiply by 3600 to convert to seconds
    hours := tonumber({Command.TotTripTime}[1 to 2])* 3600;
    
    // Parse out the minutes portion of the string and
    // multiply by 60 to convert to seconds
    minutes := tonumber({Command.TotTripTime}[4 to 5]) * 60;
    
    // Parse out the seconds
    seconds := tonumber({Command.TotTripTime}[7 to 8]);
    
    // Add up all the seconds
    hours + minutes + seconds;

    2: TotalSeconds:

    Code:
     sum({@ConvertTimeStringToSeconds})
    3: CovertTotalSeconds:

    Code:
    local numbervar RemainingSeconds;
    local numbervar Hours ;
    local numbervar Minutes;
    local numbervar Seconds;
    
    //divide the @TotalSeconds by 3600 to calculate
    // hours. Use truncate to remove the decimal portion.
    Hours := truncate({@TotalSeconds} / 3600);
    
    
    // Subtract the hours portion to get RemainingSeconds
    RemainingSeconds := {@TotalSeconds} - (Hours *
    3600);
    
    // Divide RemainingSeconds by 60 to get minutes.
    // Use truncate to remove the decimal portion.
    Minutes := truncate(RemainingSeconds/60);
    
    // Subtract the Hours and Minutes and what is left
    // over is seconds.
    Seconds := {@Totalseconds} - (Hours * 3600) -
    (Minutes * 60);
    
    
    // Format the hours, minutes, and seconds to hh:mm:ss
    totext(Hours,"00") + ":" + totext(Minutes,"00") +
    ":" + totext(Seconds,"00")

    But when I run the report I get a massage sayin "the string is non-numeric"

    Please help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width