DSUM function in crystal reports? - 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.

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

Display one Column as Multiple Columns

I have Table Structure Like
I am Using Visual Studio 2013 and Crystal Report 13.0.12
You could use a Cross Tab for this, but you'll get more freedom by not using one:
In your report, group by Customer Name, then by TestName. Suppress all 5 of these sections except for the Group Footer of Customer Name. Here, place Customer Name, Result, and a new formula named Sugar. And inside sugar put this code:
PREVIOUS({Result})
Assuming there's no other TestNames besides Protein and Sugar, and assuming every Customer has one value for each, you should get the desired results in the footer. Let me know if that's the case and I can update the answer to accommodate.

How do I do a CR-type formula in SQL Server Reporting services?

In Crystal Reports, I could define a formula that would evaluate for each detail line. For example, if I had a query that would return a PatientId, an ObsTerm name, and an ObsTerm value, I could define a formula called {#Hispanic} that had the value:
If {Command.OBSNAME} = "HISPANIC" Then
{Command.OBSVALUE}
Else
" "
Then, in the group footer, I could take Maximum({#Hispanic}, {Command.PATIENTID}) to see if I had gotten a value returned for the patient's ethnicity - either I'd get the value (assume only one, since that's how I built the query) or a blank.
I'm trying to convert a CR report over to SSRS 2008R2: how would I do the above? Thanks.
Add a calculated field to your data source (called 'Hispanic' or whatever) with a formula of:
=IIF(Fields!OBSNAME.Value="Hispanic",Fields!OBSVALUE.Value,"")
In your report, add a parent group to your detail row and type [Max(Hispanic)] into a field in the group row. You may then want to hide the detail row and show only the aggregate data. I think there's probably a much easier way to do what you want but it's not clear from your question.
I made the transition from Crystal to SSRS and it is a hard road. You need to unlearn all your Crystal (especially formatting).

Is it possible to programmatically iterate through a table within a formula in 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.

Flatten data inside a crystal report

Ok, this might be a weird request, but is it possible to essentially flatten my dataset inside a crystal report?
I have a datatable in C# that was created with a join, so when it hits the report its 2 records. Most of the columns have identical data, with the last few displaying a different address.
Instead of printing the detail section multiple times with mostly similar data, I need to display 1 'record' with the common data printed once, and each records address arranged next it. As in, all the common fields displayed in one area, and then next to that the address fields from the record where 'AddressType = 1', then next to that the address fields where 'AddressType = 2'
Is this a subreport thing? Because even with subreports I can't get it to only print 1 detail section with the data from just the first record.
Is this even possible with crystal? For long drawn out reasons, I can't flatten the data before it gets to the report.
Ok, someone here in the office showed me the way, so I'll put this out there.
Given data with cols A,B,C all common and D,E different across multiple rows, this is how I 'flattened' the dataset in crystal:
Create a group based on col A, and put A,B,C inside that group header - get rid of the details section entirely
Create a subreport in the group header for each row of data, in my case 2 subreports
Inside each subreport, put fields D,E. Important: There are NO links for these subreports!
For each subreport go into the Select Expert and create a condition that shows only 1 particular row of data. This conditional will have the same field for each subreport, but different values. In my case it was AddressType='A', and AddressType='B'
This will produce 1 report, with A,B,C listed once, and D,E listed once for each subreport(once for each row of data)
This was confusing, time consuming, and I hate crystal reports now more than ever.
It would be pretty ugly, but you could add a group for each common field in Group Expert and then display the data for the common fields in the last group header. So if your common fields are field1 through field5, you would create five groups and put all five fields in the group header of field5. Then you would put the unique address fields (call them field field6 through field8) in the details section.
Now the trick is getting everything to line up correctly. You can set "underlay following sections" on the group header for field5; this will cause field1 through field5 to "fall down" into the details section. You just need to make sure that field6 through field8 are all to the right of field1 through field5 so the text does not overlap.
Now, if you want the two address records to print horizontally, I think you will need a subreport with multiple columns for that. But the same principle applies -- just make sure the subreport is to the right of field1 through field5 so the data doesn't overlap.
Have you tried the suppress if duplicated option on each non-address field?
Otherwise, you could group by the common id, put the common fields in that header, and then display the multiple addresses in the details section.
Or, you could remove the addresses from the datasource and use a subreport to fetch this data for each record. This would bypass the join and be the slowest option performance wise, due to having to select the addresses for each record.
Ok, firstly let's see if I understand this right :
You want a report that would be in the format
MainDetails Address(type1)
MainDetails Address(type2)
to instead be in the format
MainDetails Address(type1) Address(type2)
?
Assuming there are only two address types, you can do the following :
1) Group by Main details (whatever the unique entry is
2) Put the address details in the group header next to them, on the right
3) Also put the address details in the details section, but positioned as if they were in the Address(type2) column positions, so it looks like :
GH MainDetails Address
D.......................................Address
4) Next, add a sort to the report on the AddressType field, so that AddressType=1 shows first.
5) Add a conditional suppression formula to the Details section saying {AddressType=1}
6) Using the Section Expert, in the Group Header tick the 'underlay following sections' box
This should work as long as the number of addresses is either 1 or 2.