Search code examples
sortingcrystal-reports

Sort by shared variable


My report group displays the net cost of the top 10 clients. {@NetCost} is a shared variable for calculate sales cost - all taxes and all discounts:

if travelType="OW" then
  salesCost-Tax1-Tax2-Tax3-Discount1-Discount2
else 
  (salesCost-Tax1-Tax2-Tax3-Discount1-Discount2)/2

shared currencyvar netCostTotal:=netCostTotal+{@netCost};

I know how to sort by one database field, but how can I sort by a running shared variable?

   client#   client name     Net Cost
1  1234      XXXXX           150.22
2  2345      XXXXX           140.11
3  4567      XXXXX           120.00

Solution

  • Shared variables open up a lot of room for error, but it should still be possible to sort by their results: Create a new formula field, and in its formula just put the shared netCost variable. From here you can insert a Group based on this new Formula field. Depending on the timing of events in the report, you might need to use whileprintingrecords in this Formula.

    In this particular case you sorted on a Summary of your netCost value and sorting by that, which earned the correct answer.