OPAL or Calculated field
Author Message
OPAL or Calculated field
Can I calculate the average miles between oil changes using a calculated
field? In OPAL? Below is my table example.

5000
10000
14000
21000

Thanks in advance for any help.
Fredrick Bartlett

Wed, 08 Jul 1998 03:00:00 GMT
OPAL or Calculated field

Quote:

>Can I calculate the average miles between oil changes using a calculated
>field? In OPAL? Below is my table example.

>5000
>10000
>14000
>21000

>Thanks in advance for any help.
>Fredrick Bartlett

Calculate field? No
OPAL? Yes
First create a custom procedure (which I named calcAvg) in the form's Procs
box as follows:

proc calcAvg() number
var
tc tcursor
i,j,avg number
ar array[] number
y smallint
x longint
endvar
;Open the tcursor on your table
tc.open("table path and name")        ; I called my table OILCHNGE, so I used
;tc.open("OILCHNGE") - It was in my current
;Working directory so I did not specify a
;path
;Find out how many records are in your table (make sure the current record
;is posted before calling this procedure)
x=tc.nrecords()
;Resize the array to the same size as the tcursor
ar.grow(x)
;Initialize the x and i variables
i=0
x=0
;Run a scan on the tcursor
scan tc :

x=x+1                   ;Increment x
ar[x]=j-i                       ;Make the xth array element = j-i
i=j                     ;Redefine i as j
endscan
;You now have an array containing the miles between chg's - now use a For
;loop to sum those differences
j=0                     ;Reinitialize j so we can use it to store the sum
for y from 1 to x               ;x still equals the number of array elements
j=j+ar[y]
endfor
avg=j/x                 ;Calculate the average
return avg              ;Quit the procedure and return avg to calling method
endproc

On your form, place an unbound (undefined) field object and name it
something - I used fldAvg. Change the field's label text to something
descriptive.

trigger the calcAvg procedure and put the result into the fldAvg field:

action(datapostrecord)  ;may or may not be necessary - wasn't on my form

;and the newvalue method was always preceded
;automatically by a postRecord.
dodefault
fldAvg.value = calcAvg()

And that's it. Hope this helps.

Bob Barrows -

Thu, 09 Jul 1998 03:00:00 GMT
OPAL or Calculated field

Quote:

>>Can I calculate the average miles between oil changes using a calculated
>>field? In OPAL? Below is my table example.

>>5000
>>10000
>>14000
>>21000

>>Thanks in advance for any help.
>>Fredrick Bartlett
>Calculate field? No
>OPAL? Yes
>First create a custom procedure (which I named calcAvg) in the form's Procs
>box as follows:

[snip]

Yes, you CAN do this in a calculated field.  The syntax is:

cAverage(fieldname)  where fieldname is a quoted string.

Bev.

Mon, 13 Jul 1998 03:00:00 GMT
OPAL or Calculated field
Quote:

>>>Can I calculate the average miles between oil changes using a calculated
>>>field? In OPAL? Below is my table example.

>>>5000
>>>10000
>>>14000
>>>21000

>>>Thanks in advance for any help.
>>>Fredrick Bartlett

>>Calculate field? No
>>OPAL? Yes
>>First create a custom procedure (which I named calcAvg) in the form's Procs
>>box as follows:
>[snip]

>Yes, you CAN do this in a calculated field.  The syntax is:

>   cAverage(fieldname)  where fieldname is a quoted string.

>Bev.

Hate to disagree with you, Bev, but that will give you the _overall_ average;
the average value for the samples given would be 12,500: (5000+10000+14000+21000)/4
what he's looking for (if I understand the original post correctly)
is the average value of the _differences_ of the sequential values, in other words
(5000 - 0)
(10000 - 5000)
(14000 - 10000)
(21000 - 14000)

Actually, now that I think about it, you _could_ do it using a calculated field;
you'd have to add two more fields, [Previous Mileage] and [Difference].  Previous
Mileage would hold the mileage reading at the last oil change (0 if it's the first
oil change), and difference would hold Mileage - Previous Mileage.  Then do a
caverage on "Difference"; example:
Mileage    Previous Mileage   Difference
5000       0                  5000
10000      5000               5000
14000      10000              4000
21000      14000              7000

*then* caverage("Difference") would give you 5025.  Of course, this approach opens
a whole new discussion about storing calculated values in a relational database,

Jim

Tue, 14 Jul 1998 03:00:00 GMT
OPAL or Calculated field
Jim,

Your custom proc. worked perfectly. It was exactly what I was looking for.

Thanks Again,
Fredrick Bartlett

Thu, 16 Jul 1998 03:00:00 GMT
OPAL or Calculated field

Quote:

>>>>Can I calculate the average miles between oil changes using a calculated
>>>>field? In OPAL? Below is my table example.

>>>>5000
>>>>10000
>>>>14000
>>>>21000

>>>>Thanks in advance for any help.
>>>>Fredrick Bartlett

>>>Calculate field? No
>>>OPAL? Yes
>>>First create a custom procedure (which I named calcAvg) in the form's Procs
>>>box as follows:
>>[snip]

>>Yes, you CAN do this in a calculated field.  The syntax is:

>>   cAverage(fieldname)  where fieldname is a quoted string.

>>Bev.

>Hate to disagree with you, Bev, but that will give you the _overall_ average;
>the average value for the samples given would be 12,500: (5000+10000+14000+21000)/4
>what he's looking for (if I understand the original post correctly)
>is the average value of the _differences_ of the sequential values, in other words
>(5000 - 0)
>(10000 - 5000)
>(14000 - 10000)
>(21000 - 14000)

>Actually, now that I think about it, you _could_ do it using a calculated field;
>you'd have to add two more fields, [Previous Mileage] and [Difference].  Previous
>Mileage would hold the mileage reading at the last oil change (0 if it's the first
>oil change), and difference would hold Mileage - Previous Mileage.  Then do a
>caverage on "Difference"; example:
>Mileage    Previous Mileage   Difference
>5000       0                  5000
>10000      5000               5000
>14000      10000              4000
>21000      14000              7000

>*then* caverage("Difference") would give you 5025.  Of course, this approach opens
>a whole new discussion about storing calculated values in a relational database,

>Jim

If you want the average INTERVAL then simply divide the last value
by the number of changes.  cMax( ) / cCount(). (21000/4)

--
--------------------------------------------------------------------

Coupled Climate Dynamics Group/971
NASA Goddard Space Flight Center
Greenbelt, MD 20771

Fri, 17 Jul 1998 03:00:00 GMT
OPAL or Calculated field

Quote:

>   If you want the average INTERVAL then simply divide the last value
>by the number of changes.  cMax( ) / cCount(). (21000/4)
>--
>--------------------------------------------------------------------

>  Coupled Climate Dynamics Group/971
>  NASA Goddard Space Flight Center
>  Greenbelt, MD 20771

<BLUSH>
Oh my god, you're right!!!!

duhhhhh

I guess I let my enthusiasm for wiiting a slick piece of code (IMHO)
override my brains here.

Sorry Frederick - use this instead - probably be quicker than my custom
procedure.

Sun, 19 Jul 1998 03:00:00 GMT
OPAL or Calculated field
Quote:

>>Actually, now that I think about it, you _could_ do it using a calculated field;
>>you'd have to add two more fields, [Previous Mileage] and [Difference].  Previous
>>Mileage would hold the mileage reading at the last oil change (0 if it's the first
>>oil change), and difference would hold Mileage - Previous Mileage.  Then do a
>>caverage on "Difference"; example:
>>Mileage    Previous Mileage   Difference
>>5000       0                  5000
>>10000      5000               5000
>>14000      10000              4000
>>21000      14000              7000

>>*then* caverage("Difference") would give you 5025.  Of course, this approach opens
>>a whole new discussion about storing calculated values in a relational database,

>>Jim

>   If you want the average INTERVAL then simply divide the last value
>by the number of changes.  cMax( ) / cCount(). (21000/4)

Well, sure, if you want to do it the *easy* way :)

Jim  <:-)

Tue, 21 Jul 1998 03:00:00 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages