问题
I'm familiar with SQL but not Crystal Reports. I'm trying to deal with an imported data set with 5 columns:
id deathDate giftDate giftAmount Dead
123 2008-01-06 2011-09-08 25.00 TRUE
456 2009-06-08 2011-10-13 10.00 TRUE
789 0 2011-12-04 50.00 FALSE
...
I'm trying to do a subquery but can't figure out what the CR equivalent of WHERE in SQL would be. I'd like to do something along the line of:
SELECT count(id) from tab1 where dead=TRUE
Any suggestions?
回答1:
As Conrad and dotjoe have observed, the Crystal equivalent of the sql where
clause is the Select Expert - you should be able to find this on the Report menu.
If you need to include both true and false Dead
records in the detail section, but want a total for only those records where Dead
is true, the simplest way to do this would be to set up a formula item. To do so:
- Right-click on the Formula Fields option in the Field Explorer and select New... .
- Enter a suitable formula field name, like
DeadCount
. In the Formula editor, enter a formula like the following (assuming
Dead
is a string):If {tab1.Dead} = 'TRUE' then 1
Use the
x-2
button (or Alt-C) to check that the formula does not have any errors, then press the Save and Close button to exit the formula editor.- Drag and drop the new formula field from the Field Explorer onto anywhere in the report.
- Right-click on the formula field that you have just added to the report and select Insert > Summary... from the menu.
- In the Insert Summary dialog, specify the Summary operation as Sum and the Summary location as Grand Total (Report Footer), then click OK. A summarised field, labelled something like
Sum of @DeadCount
, should appear in the Report Footer. (You should now remove the un-summarised formula field from where you placed it in the report design area.)
This technique is essentially similar to including a summed case value in a sql query - something like: select sum(case when Dead = 'TRUE' then 1 end) as DeadCount from tab1
回答2:
Add this to the record selection formula...
{datasetname.Dead} = true
//note: I'm not sure what data type that is but CR uses bool for bit and XSD bool
Then add a summary field to the report footer which does the count(id).
Or, if you need to display the dataset and only need a subquery you can use something called a "Running Total" field. In here you can do the count(id) and add the where clause to the necessary formula.
来源:https://stackoverflow.com/questions/9439169/crystal-reports-equivalent-of-where