Solved

Crystal Report XI - Distinct Count Where Table.Field = A

Posted on 2009-04-02
4
1,874 Views
Last Modified: 2012-06-22
I'm trying to find a solution/formula where when the Table.Field1 = A, I can then get a DistinctCount from Table.Field2.  I've tried If/Then statements and While/Do statements.
For instance, I'll create a Formula Field called @Example and put in the formula:  If {Table.Field1} = A Then DistinctCount({Table.Field2}).  It doesn't seem to add up correctly.
Then when I do the following, @Example - While {Table.Field1} = A Do DistinctCount({Table.Field2}), I get a loop error.
I've also created it where I just have @Example to be {Table.Field1} = A and then in the report itself have it do a distinct count; however, it's adding up the wrong field.

I want to total amount of Table.Field2 where Table.Field1 = A.

Thanks.
0
Comment
Question by:kristibigo
  • 2
4 Comments
 
LVL 17

Assisted Solution

by:MIKE
MIKE earned 125 total points
ID: 24051334
Well to do this very easily in SQL use this syntax:

To find SUM of the field:

select sum(Table.Field2)
from Table
where Table.Field1 = 'A'

To find COUNT of the field:

select count(Table.Field2)
from Table
where Table.Field1 = 'A'

To find DISTINCT COUNT of the field:

select distinct count(Table.Field2)
from Table
where Table.Field1 = 'A'

Will this work.....or do you not have access to the Database..?
0
 

Author Comment

by:kristibigo
ID: 24051713

Yes, however, I also just figured out another way to solve my issue.  Here is what I did:
Created in the Formula Field @Example1 where the formula reads:
If {Table.Field1} = A Then 1 Else 0
Then I went to the report to the Toolbar >Insert >Summary
Chose from the field to summarize: @Example1
Calculate this Summary: 'Sum'
Summary location: Group #?
And then clicked OK
From there, the @Example appeared in my report, and I just dragged it to the place where I wanted that result to be seen.
I was then able to do likewise for the opposite information where in the Formula Field @Example2 was created {Table.Field1} <> A Then 1 Else 0
Thank you for your input above.
0
 
LVL 17

Assisted Solution

by:MIKE
MIKE earned 125 total points
ID: 24051732
Sure,...there are usually more than one way to solve our issues.

I usually go to the datasource SQL since it speeds up the report processing...

Glad you figured it out.
0
 
LVL 34

Accepted Solution

by:
James0628 earned 125 total points
ID: 24056069
FWIW, if you haven't already figured this out, the reason that DistinctCount did not work in your "if" formula is that DistinctCount, and the other summary functions, are not ongoing things that are applied on a record by record basis.  They produce the actual summary values.  For example, every time you use Count ({field}), it gives you the count for {field} for the entire report.

 Also, While/For loops are not stepped through once for each record.  The entire loop is repeated every time the formula is evaluated.  So, ' While {Table.Field1} = "A" ' would not do anything if the field in the current record was not "A", and would be an endless loop if the field in the current record was "A".


 As for the formulas that you said you're using, they may work, but I don't think they'll give you exactly what you said you were looking for.  You said that you wanted a distinct count of Field2 where Field1 is "A".  What you're getting is just a count, not a distinct count.  If Field2 is always different, then the two counts would be the same.  But if Field2 is sometimes the same in different records where Field1 = "A", then a distinct count would be different (smaller).

 For example:

 Field1   Field2
   A           1
   A           2
   A           2
   B           1
   B           2

 Your count would return 3, for the 3 "A" records.  But a distinct count of the values in Field2 in those "A" records would give you 2.

 James
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I hate sub reports and always consider them the last resort in any reporting solution.  The negative effect on performance and maintainability is just not worth the easy ride they give the report writer.  Nine times out of ten reporting requirements…
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now