Bpath Lesson 10 Aggregations
Bpath Lesson 10 Aggregations
Hello everybody,
as indicated in the last lesson groupings and aggregations somehow belong together. So I speeded a bit
up with this lesson, which is a available a bit earlier. Content will be aggregations solely, with just one small
exception. Nevertheless it will be an extensive lesson.
Aggregations are aggregating a number of datasets to one value. The most prominent representants here
are the COUNT, SUM and the AVERAGE function. But one should not forget "easy" aggregations like MIN,
MAX, FIRST or LAST. On the other side there are also a couple of more complex aggregations as SEARCH,
GEOAVG and STDEV. The full list can be obtained in the table at the end of this blog post.
Aggregations. Practice
~*[!CLASS]/SearchResFlightRel/FlightBookRel$(@CLASS::!COUNT=Count();!SUM=Sum(@LOCCURAM))
Well, that's easy now, isn't it? Slightly easier compared to the hand-made approach.
The code explains itself. Hopefully. Count can be enhanced with a condition, so only rows
fullfilling the condition are counted. As with the sum() function the same can be achieved using a
sum(iff(condition,@Feld,0)) construction.
Product() works similar to Sum() but muliplies all available parameters. Might be useful for probabilities. Since
the result may be a fracture, possibility to round result is provided.
~*[!CLASS_LONG,!FORCURKEY]/SearchResFlightRel/FlightBookRel$(@@CLASS,@FORCURKEY::!
Average=AVG(@LOCCURAM,2);!PASSENGERLIST=CONC(@PASSNAME,",",@PASSNAME<>""))
The Average function without second parameter returns the value with 34 digits. This is probably more
exact than you need, so it is possible to add the number of decimal places (supported by all 3 "advanced
aggregations" which work on Numbers).
Conc() (please distinguish with the concatenate function, which is not an aggregation function) concatenates
the handed over (with parameter 1) value separated by parameter 2. As third parameter it is possible to specify
a condition. In this case it is used to sort out non-filled entries. In contrast to the LIST() function, this is not done
automatically.
Please note that the Group condition is a long text field here, consequently the sort condition has to reflect this
also.
Geoavg() calculates the geometrical average (nth root on the product of all elements). Calculation is done
using logarithm. All numbers in the calculations must be positive.
Minima. Maxima.
~*/SearchResFlightRel/FlightBookRel$(@@CLASS,@FORCURKEY::!
R=Min(@LOCCURAM,concatenate(@PASSNAME," paid ",""+@FORCURAM," ",@FORCURKEY)))
The example shows the usage of the Min/Max method. The result is assembled based on the first Min/Max row
found with the second parameter. If no second parameter is provided, the min/max value is returned directly.
Example 46 shows, that aggregations are not necesarilly basing on attributes directly. The example calculates
the maximum paid for a kilogram of luggage, with the side condition that the weight should be bigger than zero.
Using the iff() function is a standard way to deal with these side condition, but please note that in this case it is
actually not needed since the division is anyway defined in a way that 0 is returned if it is attempted to divide by
zero. This is done to avoid dumps at run time which can not be detected at design time.
~*/SearchResFlightRel/FlightBookRel$(*::!R=Max(iff(@LUGGWEIGHT>0,@LOCCURAM
%@LUGGWEIGHT,0)))
Some "easy" aggregations. First() returns the value corresponding to the first row, the aggregation may be
equiped with a condition . Last() basically does ... nothing. It is just included for the sake of completeness, you
will have the same effect if you state the parameter directly.
With GrpIndex() you might access the nth row (n is the first parameter, second parameter specifies the value
to be returned). In contrast to direct indexing, n has to be positive here.
Any() uses the randomizer library to choose an arbitrary row from which the return is assembled.
Search(), SearchFirst()
SearchFirst() and Search() are searching for specific rows fullfilling the specified search condition. SearchFirst
chooses the first found entry, Search all - or if no self linking is used, in consequence the last row.
~*/SearchResFlightRel/FlightBookRel$(*::!R=SearchFirst(@LUGGWEIGHT>30,@Passname))
The following example shows the teamwork of the Search and the list function. The search function searches
all bookings with "overweight" (considered to be above 24) and adds the name of the passenger to the list of
overweighted passengers:
~*/SearchResFlightRel/FlightBookRel$(@@CLASS::!OVERWGHT=Search(@LUGGWEIGHT>24,LIST(!
OVERWGHT,",",@PASSNAME)))
Code Example 48, $(*::!X=Search(Cond,func(!X))), Searches returning lists
Median()
Median() sorts the attributes and chooses the one in the middle (respectively one of the middle pair). Median is
supported for Numeric Values, Strings and Dates. Median() has no optional rounding parameter.
Even though it sounds similar to the average, technically it is a bit more costly. For all other aggregations it is
sufficient to keep one value per group, for the Median the complete list has to be kept. Performance seems to
be harmed only by a smaller factor as the sort_in features of ABAP tables are used.
~*/SearchResFlightRel/FlightBookRel/...$/*
Additional to the relation to the object itself ("."), the relation to the parent (".."), the relation to the root object is
now available using the ... syntax. In contrast to the parent, the root is unambigous.
Aggregation Table
The letters refer to the 4 possible datatypes ( String, Date, Boolean, Number ). The GRPINDEX function for
example has a first parameter of type Number follwed by a second parameter of any type. Conc has either two
parameters of type String, or two string parameters followed by a Boolean parameter as third parameter.
Name Main Parameters Optional Parmeters Description
COUNT no B counts all elements where
the condition evals to true
SUM N no sums up handed over
value
you actually caught me on the wrong foot, as I thought I finished the series. But you are right, I missed about
lesson 11, which I think will be the last lesson. As I have quite some work due to a conference next week, I
might take a look in October what I am going to publish.
Hope that's fine. Please apologize the long waiting period for the lesson.
thank you very much for your blog! I am very interessted in the next lesson 11 about sub-fuctions. When do
plan to publish it? I did some work arounds because I do not know how to use the sub-functions.