Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: language manual Post Reply Post New Topic
Author Message
nagileonc
Newbie
Newbie


Joined: 08 Dec 2007
Online Status: Offline
Posts: 3
Quote nagileonc Replybullet Topic: language manual
     Posted: 08 Dec 2007 at 4:41am
Hi guys
I'm looking for comprehensive crystal programming language tutorial.
Can you please send a link ?

I'm struggling with this:

if {leads.created_at} >{?date_from} and {leads.created_at} <{?date_to} then
Sum({leads.cost}) + Sum({leads.affiliate_bounty})
else
0


I'ts totaling all values - but I need to count only those in specific period eg 01/11/2007 - 30-11-2007.

Best Regards

Peter
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 08 Dec 2007 at 2:19pm
I have three chapters covering writing Crystal Reports formulas. I also cover Crystal Syntax and Basic Syntax. Check out chapter 5-7 and Appendix B in my book Crystal Reports Encyclopedia.

Re your question, this actually looks fine upon inspection and I can't say why it doesn't work. You could also try using the IN operator for ranges.
if {leads.created_at} IN {@date_from} _TO_ {@Date_To} Then...

Notice that I use underscores around the TO operator. This means to NOT iinclude the beginning and ending dates as part of the range (similar to '>' and '<'). If you wanted to include the dates as part of the range, then drop the underscores (i.e. just use 'TO').
If this still doesn't work, then something else is causing you problems. The most common cause is that you have a null value in the date field. Set the Report Option to Convert Nulls to Default Values and see if this helps.
If this still causes you problems, then I would debug it by first determining why the If statement isn't working. I would create a formula that just returns the result of the If statement and print that in the Details section. I do that because then I can just focus on the If statement and tweak it until it returns True and False on the records I expect it to. After I get that working then I add the rest of the formula which would do the Sum() functions.
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
nagileonc
Newbie
Newbie


Joined: 08 Dec 2007
Online Status: Offline
Posts: 3
Quote nagileonc Replybullet Posted: 09 Dec 2007 at 1:57pm
Hi there
 
I don't have problem with date ranges - this part works perfectly fine.
But each time the condition is true - all records are being summed which is not walid - I nead to sum only those in specified date range.
 
Best Regards
 
Peter
 
IP IP Logged
Lugh
Senior Member
Senior Member
Avatar

Joined: 14 Nov 2007
Online Status: Offline
Posts: 377
Quote Lugh Replybullet Posted: 10 Dec 2007 at 4:37am
This is an extremely common problem in Crystal.  The basic issue is that your If..Then structure is outside your Sum function.  Hence, Crystal is still taking the sum of all the records.

The simplest solution is to create a new formula, applied to each record.  It would look something like:

if {leads.created_at} >{?date_from} and {leads.created_at} <{?date_to} then
{leads.cost} + {leads.affiliate_bounty}
else
0


Then, you can simply take the Sum of that formula.  This should give you the kind of result you are looking for.
IP IP Logged
nagileonc
Newbie
Newbie


Joined: 08 Dec 2007
Online Status: Offline
Posts: 3
Quote nagileonc Replybullet Posted: 11 Dec 2007 at 1:38am

Cool

But I'm still looking how to do :
 
Sum of (leads.affiliate_bounty + leads.cost) in specyfic date range.
 
So I can show ithe total amount in one cell (field).
 
eg of the report:
 
Date_From      Date_To       Zero_Leads         Paid_Leads    Total_Cost
----------------|---------------|-----------------|----------------|-----------------
01-11-2007     30-11-2007      3074              6849              £100894
 
Where:
Zero_leads = Sum Of All Rows where the waluae is 0 in given date range
Paid_Leads = Count Of All Rows where the waluse is > 0 in given date range
Total_Cost = Sum Of All Rows containing (leads.affiliate_bounty + leads.cost) in given date range
 
 
So I was trying: Sum ({leads.cost} + {leads.affiliate_bounty})  but it gives me an error: "A field is required here"
 
Best Regards
 
Peter


Edited by nagileonc - 11 Dec 2007 at 1:45am
IP IP Logged
Lugh
Senior Member
Senior Member
Avatar

Joined: 14 Nov 2007
Online Status: Offline
Posts: 377
Quote Lugh Replybullet Posted: 14 Dec 2007 at 5:25am
Again, Sum only works on a single field.  You can't take a Sum (or any other aggregate) of an expression.

The simplest solution is to create a formula that does the math for each record, then do a Sum on that.  (As an important caveat, depending on exactly what your formula is doing, you may not be able to perform a Sum on it.)  This is what I did above.  While it looks counter-intuitive, it really is the cleanest way in Crystal.  Note that you don't actually have to put the formula itself on the report anywhere.  Crystal doesn't make you "show your work."  Wink

So, create three formulas:

@Zero_Lead
IF {leads.leaddate} IN {?Date_From} TO {?Date_To} AND
     {leads.leadvalue} = 0 THEN 1
ELSE 0


@Paid_Lead
IF {leads.leaddate} IN {?Date_From} TO {?Date_To} AND
     {leads.leadvalue} > 0 THEN 1
ELSE 0


@Lead_Cost
IF {leads.leaddate} IN {?Date_From} TO {?Date_To}
THEN {leads.affiliate_bounty} +  {leads.cost}
ELSE 0


In your Report Footer (assuming that's where you want it), your fields would look like:


Date_From      Date_To       Zero_Leads           Paid_Leads        Total_Cost
--------------|-------------|--------------------|-----------------|----------
{?Date_From}   {?Date_To}    SUM({@Zero_Lead})    SUM({@Paid_Lead}) SUM({@Lead_Cost})




Hopefully that's a little clearer.

Incidentally, if you are grouping your report by month, rather than entering your beginning and ending dates as parameters, it's even easier.  Just remove the date criteria in the formulas (in the case of @Lead_Cost, just remove the If..Then structure altogether, and just leave the addition).  Group on your dates by month, and put the Sum values in the Group Footer.  You can even suppress the details sections to make it all look like one big pretty table.
IP IP Logged
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