Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Status/Time Period Summing Post Reply Post New Topic
Author Message
ninanderson
Newbie
Newbie
Avatar

Joined: 18 Dec 2011
Online Status: Offline
Posts: 3
Quote ninanderson Replybullet Topic: Status/Time Period Summing
    Posted: 18 Dec 2011 at 10:27pm
Hello,
 
Being new to Crystal reports I'm hoping that someone can point me in the right direction with a problem I have.
 
I have data coming from a machine to show it's running status. This is indicated by an integer number in the range 1-10 (1= Stopped, 2= Running, 3= Waiting for material, 4 = Alarm etc). A new time stamped record is made every time the machine changes status.
 
I need to report how long the machine was at each state in any given period but I can't figure a way to extract and summ the data.
 
Example data looks like this:
 
Date  Time  Status
 
11/12/11   08:56:32   1
11/12/11   08:56:52   2
11/12/11   08:58:52   4
11/12/11   08:59:24   1
11/12/11   09:07:22   2
11/12/11   09:53:22   6
11/12/11   11:26:12   9
11/12/11   13:36:32   1
11/12/11   15:54:42   2
11/12/11   19:06:32   6
11/12/11   19:06:52   1
11/12/11   19:26:52   2
11/12/11   20:26:36   6
12/12/11   03:46:22   4
12/12/11   08:56:32   1
12/12/11   09:16:32   2
12/12/11   09:26:32   6
12/12/11   09:46:22   9
12/12/11   10:06:12   8
12/12/11   10:16:32   1
12/12/11   10:16:52   2
12/12/11   10:34:32   4
 
I would expect the report to show:
 
Status             Total Time
1                     3:06:48
2                     5:27:14
4                     5:10:42
6                     9:12:46
8                     0:10:20
9                     3:30:10
 
 
Any advise would be much appreciated
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 19 Dec 2011 at 3:38am
since you know that there are only 10 states, I would use an array and increment it as needed. Something like:
 
report header:
shared numbervar array stateMinute;
redim stateMinute[10];
 
in the detail:
shared datetime lastDate;
local datetime thisDate := {table.field};
shared int lastStatus;
 
stateMinute[lastStatus] = datediff("s", lastDate, thisDate);
lastStatus := {table.status};
"" //hide the output of the formula
 
in footer:
shared numbervar stateMinute;
local stringvar result := "";
local numbervar ind;
for ind = 1 to 10 step 1 do(
  if stateMinute[ind] <> 0 then (
     result := result + totext(ind, 0,"") + chrw(9);   //status and tab
     result := result + formula to format the secondsas desired + chrw(13) + chrw(10);   //seconds formated as hours:minutes:seconds crlf
  );
);
result
 
 
place the display in a text field that can grow.
 
it should be close, I don't remember precise syntax or the order of values in teh datediff function, but my goal is to give a path to a solution.
 
IP IP Logged
ninanderson
Newbie
Newbie
Avatar

Joined: 18 Dec 2011
Online Status: Offline
Posts: 3
Quote ninanderson Replybullet Posted: 19 Dec 2011 at 4:32am
Thanks Lockwelle,
 
I'll give a go and learn from your guidance
 
 
IP IP Logged
ninanderson
Newbie
Newbie
Avatar

Joined: 18 Dec 2011
Online Status: Offline
Posts: 3
Quote ninanderson Replybullet Posted: 22 Dec 2011 at 12:13am
Lockwelle,
 
I've used your code and with a little modification it now works !
It's been a steep learning curve but I'm begining to get to grips Crystal Reports and can see me using it a lot as time goes by.
 
Thanks again for your invaluable help.
 
The finished codeis this:
 
Header Section
 
// Flag set to indicate first record being read
    shared booleanVar FirstRecord:= TRUE;
// Array to hold accumulated time periods in seconds for each status (0-10)
    shared numberVar array StatusArray;
    redim StatusArray [11];
// Status descriptions (0-10) held in second array
    Shared stringVar array StatusNames := [
    
        'Unknown                            ',
        'Stopped                            ',
        'Stopped in alarm               ',
        'Status 3 (Future)              ',
        'Status 4 (Future)              ',
        'Status 5 (Future)              ',
        'Status 6 (Future)              ',
        'Waiting for upstream line     ',
        'Running in alarm               ',
        'Running empty                  ',
        'Running                            '
        ];
 
// Hide the output
    ""
 
 
Deatil Section
 
// Variable declarations
    // External from header
    shared booleanVar      FirstRecord;
    shared numberVar array StatusArray;
    // Last Scan
    shared dateTimeVar  LastTime;
    shared numberVar    LastStatus;
    // This Scan
    local dateTimeVar  ThisTime;
    local numberVar    ThisStatus;
    // Scan Results
    local numberVar    Status;
    local numberVar    Period;
    // Totaliser
    local numberVar    Status;
    local numberVar    Period;
    local numbervar    Index;
 
// Read time and status from current record
    ThisTime    := {LinesStatus.TimeCol};
    ThisStatus  := {LinesStatus.Status};
  
// Check for first record
    IF FirstRecord = TRUE
    THEN
    // Take a snapshot of current record on first scan
   (LastStatus  := ThisStatus;
    LastTime    := ThisTime)
 
    ELSE
    // Calculate period and status on all other scans
  ( Period      := datediff("s", LastTime, ThisTime);
    Status      := LastStatus;
    LastStatus  := ThisStatus;
    LastTime    := ThisTime);

// Reset first record flag
    FirstRecord:= FALSE;

// Assemble results into an array (Offset by 1 to cope with Status 0)
    StatusArray[STATUS + 1]:= StatusArray[STATUS + 1] + Period;

// Hide the output
   ""
 
Footer Section
 
 
// External variable from header
  
    shared numberVar array StatusArray;
    shared stringVar array StatusNames;
//  Result string
    local stringvar Result := "";
// Loop to assemble each status/period 
    local numbervar i;
       
    FOR i := 1 to 11 step 1 do(
        if StatusArray <> 0 then (
            Result := Result & totext((i-1), 0,"") & chrw(9);   // Status number and tab
            Result := Result & StatusNames & chrw(9);        // Status description and tab
            Result := Result & totext((StatusArray), 0,"");  // Period at status (in seconds) and tab
            Result := Result & " Secs" & chrw(13) & chrw(10);   // Add units and LF/CR
        );
    );
// Output the result
    Result
 
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.016 seconds.