Conditional Count() in crystal report - crystal-reports

How to get the result using formula field in crystalreport
for counting number of records where fieldname =xxx
like: select count(*) as cnt from tbl where f1=xxx
and explain more with examples for crystal report count() function.
Project: VB.Net
regards,
Sensa.

you basically create a formula field like:
if {mytable.field} = 'xxx' then
{mytable.field};
then count({formula});
or use running totals with an evaluation formula: {mytable.field} = 'xxx'

You can try this:
FormulaField1:
if {anytable.yourfield} = 'xxx' then 1 Else 0;
And then:
FormulaField2:
sum({formula1});
For integer:
FormulaField2:
CSTR (sum({formula1}),0);

There are several alternatives each with certain drawbacks.
1) Use the select expert to restrict the records in the report conditionally.
The major drawback in this case is of course loss of data probably relevant to other parts of the report
2) Solution 1) using a sub-report integrated in the main report.
The problem in this case is formatting constraints
3) Use grouping. I.E. group by mytable.field use a running total on the records
withing the group and insert to group header. Hide the group selectively trough the section expert.
Here again arises the problem of additional formatting constraints.
4) Finally if you have access to the actual database. Create a view, this will be the best solution in terms of performance and issues with formatting within crystal reports.
However creating a view each time this kind of problem arises can lead to polluting your database with views which will only be used infrequently.

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.

SSRS parameter grouping

I'm created 2 reports in SSRS 2008. I now plan on merging the reports into one and allow users to select report type via a drop down option.
The reports bring back the same data but are grouped differently.
Here lies the problem, I can't figure out how to alter the grouping depending on the report type.
Basically I want to do this
when Parameters!Report_Type.Value = "D" group by Fields!txiii_commodity.Value else no group
I've tried iif, and switch but never get the desired results.
Could someone please help.
Thanks in advance
Chris
I did this with a very simple example. Sample dataset:
Create a report with one group:
Grouped on expression:
=IIf(Parameters!Report_Type.Value = "D", Fields!txiii_commodity.Value, "Ungrouped")
Also used this to display the Group column in the report.
Results:

Conditional Formula Bombs Crystal Report

I have an issue with one of my crystal reports and it's conditional formula. The formula is shown below. I have two tables that hold different customer info that link to my invhdr table. when the account code is cash I need to retrieve a field from one table and visa versa.. The trouble is crystal seems to ignore the conditional formula as written below and seems to require that a link to the field exists even though the formula should prevent the requirement to retrieve the field..
can anyone explain a possible workaround. maybe a conditional table link if it's possible? Thanks ND
if Len("" + {invhdr.ACCT}) > 0 then
if {invhdr.ACCT} = 'CASH' then {CashCust.CUSTOM1}
else if {invhdr.ACCT} <> 'CASH' then {Lookup.VATREGNO}
else "";
so say if invhdr.ACCT ='test' it seems to still need a link to {CashCust.CUSTOM1}
all my tables are linked as inner join , not enforced
I suggest changing the inner joins to left outer joins (from the invhdr table to the CashCust and Lookup tables, so that invhdr is on the inside of the joins). This is unrelated to the formula - if you have inner joined all three tables, then corresponding records must exist on all three tables for rows to be returned; this is the definition of an inner join.
You may then encounter issues with the formula if invhdr.ACCT is null - a quirk of older versions of Crystal was that if any part of a formula evaluated to null, then the whole formula evaluated to null. This behaviour may have been amended in more recent versions of Crystal, but if not then "" + {invhdr.ACCT} may evaluate as null - try checking the value with the IsNull function instead.

Crystal Report-Running Total

I have a problem with running Total in Crsystal report9
if their is no values available for a Paticular field how can we return the running total as '0'
Instead of display the Running Total directly in your report create a Formula Field based on the Running Total and drag it into the report.
Your formula should look like this (Crystal Syntax)...
if ISNULL({#RunningTotalField}) then
"0.00"
else
ToText(RunningTotalField, 2)
If there is no data for that particular group, then Crystal won't show it easily. Your options are :
1) Use subreports to display the values for a particular group, and keep the main report just looking at the table(s) containing the group headers.
2) Use a stored procedure as the source so you have full control over the SQL that is run.
The problem is that as soon as you use a field to group, Crystal will only return records where that field has been used. If it was simply in the Details section you could modify the link to a LEFT JOIN and it wouldn't matter, but the group forces the INNER JOIN which means the groups without data are not returned.
Unfortunately Running Totals don't show up if there are no records that match your criteria. An alternative is to use a set of formulas calculated in various sections of the report. The technique is widely described in the Crystal literature. For example, this TekTips gives a very succinct overview of your options.
You set up an initialising formula in each header, with the evaluation time directive "WhilePrintingRecords". This approach was the only one available for doing running totals in the "good ol' days" before RunningTotal objects were available.