Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Static" variables? 1

Status
Not open for further replies.

aaronburro

Programmer
Jan 2, 2003
158
US
I want to create a buying cycle report. I'm a programmer by trade, so I know what to do, as far as what has to be accomplished, but I am a bit new to Crystal. Ideally, I'd love to be able to set up some variables as a kind of "static" thing, in order to hold the first and last time a product was bought, or maybe just the last three or four times, and then take the average time between purchases.

Again, i know how to mathematically accomplish this, and, given direct access to the data, I could do this all day long in my sleep! But Crystal seems to be one big old "for" loop which effectively prevents me from having direct read access. In addition, there doesn't seem to be any way to make a complex data structure, such as one that can grow to accomodate a potentially large set.

I've seen the "global" and "local" variables and am greatly intrigued... are these the way to go?
 
If you wanted to show the average difference between purchase dates for a set of products, you would first limit the records in the selection formula to those dates under consideration and then group on {table.productID}, and then create the following formulas:

//{@reset} to be placed in the group header (ProdID):
whileprintingrecords;
numbervar diff := 0;
numbervar cnt := 0;
numbervar ave := 0;

//{@accum} to be placed in the detail section:
whileprintingrecords;
numbervar diff := diff + (if {table.productID} = previous({table.productID}) then datediff("d",previous({table.date}),{table.date});
numbervar cnt := cnt + 1;
numbervar ave;

if cnt > 1 then
ave := diff/(cnt-1);

//{@displayave} to be placed in the group footer (ProdID):
whileprintingrecords;
numbervar ave;

If you wanted to compare the first and last purchase date, you could use a formula like:

datediff("d",minimum({table.date},{table.productID}),maximum({table.date},{table.productID}))

If you wanted to average these across products, you would set up formulas like the above, but at a higher level. If you were comparing average differences in purchase times for a set of products per customer, you would place the reset formula in the customer group header, the {@accum} formula in the product group header or footer, and the display formula in the customer group footer.

-LB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top