Is it possible to programmatically iterate through a table within a formula in crystal reports? - crystal-reports

Is it possible to programmatically iterate through a table within a formula in crystal reports?
E.G. If I have a master table and a detail table can I iterate through the detail table e.g.
( psuedo code )
local numberVar Total := 0
While not EOF()
IF Type = "+"
Total = Total + Quantity
ELSE IF Type = "-"
Total = Total - Quantity
ENDIF
<Next Record>
End While

It is possible to use loops in a formula field to get the data that you need, but I have only really used them in complex string manipulation. If you really need to do this then you may be able to look into using subreports, but you'd need to put the subreport in the details section which is not really recommended because it is essentially like running a new report for each record that is pulled.
It sounds like what you are really looking for is a running total field. If you join the master and details tables together the report will pull all of the rows for both tables. Then you can group by "Type" and create a running total field that sums the "Quantity" field for each row and resets on the change of the group. I think it is a little different way of thinking than traditional programming. Hope this helps.

Related

jasper report show count on footer of a specific query

am very new to jasper report
also I have tried looking at videos but can not seem to get this one concept
basically there is this main query which i have
select * from table
which is populated in the details area
however i want a second query
select count(*) from table where name = "tim"
and put the count on the footer
can this be done using jasper
any tutorial to this concept or guidance would be helpful
to sum up the details area should show all the data where as the footer should only show counts of a few things.
You can only have one DataSet (therefore query) for the report. In your case this is your main report select * from table, which seems to be working well.
You have two options for adding the information you want:
(and I would say the better option) is to add a variable $V{tim_count} which is configured as:
initial value 0
expression value "tim".equals($F{name}) ? 1 : 0"
calculation function sum
there are multiple ways to increment this variable, so I'll leave that with you. In the footer you would then add a text field with the $V{tim_count} variable as it's contents.
You can read about variables here https://community.jaspersoft.com/wiki/variables
You can add an object that has it's own DataSet:
Table
List
Subreport
You would then be able to add your query to that object and display it appropriately. As you can see, displaying a COUNT is not really the most appropriate way to do this.
Note - I don't suggest this way

DSUM function in crystal reports?

I am trying to convert an old Microsoft Access report into Crystal reports. I have everything working perfectly except for this last small detail. The Access report uses a DSUM function within an if statement for one of the fields on the report.
After much searching, I've determined that CR doesn't have anything similar.
Here's basically what I'm dealing with.
I have a proposal report. In the details of the report I print the qty, description, and a couple of price fields.
The data looks like something this:
Proposalnum Partitem RolltoItem Unitprice
18611.............1.......... NULL........0.00
18611.............2......... NULL.......17225.92
18611............3............ 2............156.90
18611............4............. 2............482.05
What I need to do is when I print a specific part, I need to query through the rest of the records to find the parts that have a matching number in the rolltoitem field and add the unitprice to the part I'm printing.
So in this example when I print partitem #2, I need to add the 156.90 and the 482.05 from parts 3 and 4 to the 17225.92 so I print a total of 17864.87.
Is there any way to do this?
As far as i know, there is no such function.
But I would try this.
The general idea is: group the data by ProposalNum and use a subreport to select the "children rows" and sum the "children prices".
Details:
Create an empty group section by PartItem.
If you want to show only items where RoolToItem is null, use a suppress function for this case.
In the details section, put a subreport. The data source of the subreport would be the same of the main report.
Change subreport links to select data in subreport based on fields: PartItem in the main report = RolltoItem in the subreport.
Pass other fields to the subreport without select data: ProposalNum, PartItem, UnitPrice. I think you need to create parameters in the subreports before doing that - example: ParentProposalNum, ParentPartItem, ParentUnitPrice.
Create a new formula: ParentUnitPrice + Sum ({YourDataSource.UnitPrice})
Put the formula in the subreport footer a long with the other fields. Maybe: ParentProposalNum, ParentPartItem, formula.
It is a theoretical solution. I hope it points out to the right direction.
If you are trying to sum the Unitprice column for all items that have the same value in Rolltoitem, you could do this with a SQL Expression Field. The code would look something like this. My Where clause may need tweaked though since I'm not sure what your database structure looks like.
(
Select Sum("YourDataBaseTableName"."Unitprice")
From YourDataBaseTableName
Where "YourDataBaseTableName"."Rolltoitem" = *currentRolltoitemValue*
)
Syntax can also vary for SQL Expression Fields based upon what type of database you are using. The syntax I provided is fairly general, but should work on SQL Server.
EDIT: Adding example with explanation of how it works.
Here is one of my SQL Expression Fields from a crystal report that prints a Bill of Lading for shipped goods.
(
Select Sum("SHIPMENTS"."PALLET_COUNT")
From SHIPMENTS
Where "SHIPMENTS"."BOL_ID" = "BOL"."ID"
)
In my database the BOL table is the starting point. A single BOL can contain 1 or more SHIPMENTS, and a single SHIPMENTS can contain one or more PRODUCTS.
Top level grouping is on BOL.ID. The PALLET_COUNT is found once and only once on each SHIPMENTS. I also had a sorting requirement for the data in the details section that prevented me from using a Running Total Field.
This allows a BOL with 2 SHIPMENTS that contains a total of 3 products to look like this:
BOL.ID SHIPMENTS.ID SHIPMENTS.BOL_ID PALLET_COUNT PRODUCT.ID
1 10 1 2 XXX
1 9 1 1 YYY
1 10 1 2 ZZZ
The correct PALLET_COUNT for this BOL should be 3, since PRODUCTS XXX and ZZZ are in the same SHIPMENTS and the PALLET_COUNT is duplicated because of its relationship to the PRODUCTS.

Crystal Reports Cross-Tab Column Totals as Variables

I have a report within Crystal 2008 that has 2 subreports, each of which is a Crosstab. I have split them into different reports as their selection and database queries are unrelated.
What i need to be able to to is to create a variable for each of the Column Totals and be able to pass this onto a third report for each of the two cross Tabs.
The Layout of each cross tab is formatted the same, with the columns being the PO Number and the rows being charges against each PO. It is the total of the columns that I need to perform a further calculation on.
Total of Crosstab1 Column1 - Total of Crosstab2 Column1 for each column that is displayed by the selection query to give me a difference between the two crosstabs.
I have tried using the CurrentFieldValue but this only appears to set the total of the very last record to the variable.
I hope that there is a way to do this and that i have provided enough information for you to be able to assist me.
I think its hard to get the value from crosstab but one workaround would be.
Create a shared variable in both subreports and assign the summary value to the shared variable.
Suppose cross tab is showing employee salary sum then in the formula assign the value to the shared variable as
Shared Numbervar report1;
report1:=Sum(employee.salary)
similarly do in the second subreport aswell.
Now in main report create a formula
Shared NumberVar report1;
Shared NumberVar report2;
report1+report2;
let me know how it goes.

Conditional Running Total in Crystal Reports

Using VS 2008 Crystal Reports, I would like to do a running total on a formula that is calculated on a group change. When I click on add a running total, this formula does not appear in the Available Tables and Fields list.
This is the logic:
On Change Group of group
if CalculatedValue > 0 then
ReportRunningTotal1 += CalculatedValue
else
ReportRunningTotal2 += CalculatedValue
Can I specify a condition in a running total? If not, how else could I do this?
More info: I am doing a running total called GroupRunningTotal of the value of db field BillableHours. At change of group, I am comparing GroupRunningTotal to a db field for that group MaxHours, and I display a result of MaxHours - GroupRunningTotal at the group level.
Appropriate today - Think of it like the electoral college - the person who wins the election does not depend on total number of votes, but of number of votes in the electoral college.
I'm interpreting your question to mean that you want to add up all the negative values in one running total (RT_Neg) and all the positive values in another (RT_Pos). How about this:
Make the RT_Neg running total. Under Field to Summarize, sum your {Tbl1}.{Amount}. Under evaluate, enter "{Tbl1}.{Amount}<0" as your custom formula. Never reset.
Make the RT_Pos running total. Under Field to Summarize, sum your {Tbl1}.{Amount}. Under evaluate, enter "{Tbl1}.{Amount}>0" as your custom formula. Never reset.
Insert both running totals in the group footer (if you put them in the header, it may not sum properly)
Alternatively, you can:
Make a custom formula "If {Tbl1}.{Amount}<0 then {Tbl1}.{Amount} else 0" and make a running total based off that.
I think one of these 2 options will get you to your goal.
You most likely cannot use one RT field as condition for other RT field. You can use formulas, placed on group footer and evaluated 'whileprintingrecords()'; in these formulas you can assign/sum into some variables and display these variables at the end of report. About like next (generic idea only, you need initialization and display routines as well):
numbervar rtcurrent := sum({somefield}, {groupfield});
numbervar rtplus;
numbervar rtminus;
if (rtcurrent > 0)
then rtplus := rtplus + rtcurrent
else rtminus := rtminus + rtcurrent;

Referencing the last row's data in Crystal Report

I have a report where the fields come from data pulled from a SQL server. I have a total field that i have added to the report...however i have an interesting conundrum--i was hoping someone had a formula i could use.
Three of my columns do NOT need summation...however my project requirements are telling me instead to just pull the last number from the last row in the report and putting that in the total row. To better clarify:
1999 0.1% 0.2% 0.3%
2001 -2% 0.3% 3.4%
Basically, in the total field, i'd be pulling the values from 2001 since it is the last report row. In my total row, i want to have -2%, 0.3% and 3.4% showing (since those are the last row's values). SO basically, i just want to pull the last report row's data (NOT total it).
Anybody have a formula i can use for this?
If I read this correctly, I typically would just put the fields themselves in the footer, without any formulas. The report should display the values from the last record in the group.
Well, I have two formulas you can use... I think the only way to do this is with a variable to capture the final value in the details section, and then display it in the group footer. So, in details, create formula field like this:
shared CurrencyVar lastValue;
if (OnLastRecord) then
lastValue := {my_table.field_name}
Add this to your details section and suppress it (so it doesn't display). Then add another formula like this:
shared CurrencyVar lastValue;
lastValue;
Add this to your group section where the total would normally go.
You will need another set for formulas for each field you need to handle this way. Note that you could handle all the fields in the first formula if you use basic syntax (so you can have multiple statements under the 'if').
I would like to share my example.
I should keep last modified value in details and represent in group.
In details I have:
shared NumberVar X1;
if {Q.SklID}={#InSkl8} AND ({Q.Poredak}=1 OR {Q.Poredak}=2) then X8:={Q.MCena};
and in group I have following formula:
shared NumberVar X8;
X8;
This way, I escape situation to have NULL in group formula because I don't have else clause in details formula.