Version: 6.x

# How-to: GROUP MAX/MIN/AGGR

## Example 1​

We have a set of books, where each book has a unique ID.

``CLASS Book 'Book';id 'Number' = DATA INTEGER (Book);``

We need to find the maximum book ID.

### Solution​

``maxId 'Maximum number' () = GROUP MAX id(Book b);``

## Example 2​

Similar to Example 1.

We need to find a `Book` object by book ID.

### Solution​

``// Option 1book1 'Book' (INTEGER i) = GROUP MAX Book b BY id(b);// Option 2book2 'Book' (INTEGER i) = GROUP AGGR Book b BY id(b);``

The difference between Option 2 and Option 1 is that declaration of this property puts a constraint on the uniqueness of book IDs. Any attempt to add two or more books with the same ID will result in the error message.

## Example 3​

We have a set of books, where each book is associated with a category and price.

``CLASS Category 'Category';category 'Category' = DATA Category (Book);price 'Price' = DATA NUMERIC[10,2] (Book);``

We need to calculate the minimum price per category.

### Solution​

``minPrice 'Minimum price' (Category c) = GROUP MIN price(Book b) BY category(b);``

## Example 4​

``CLASS Shipment 'Shipment';CLASS ShipmentDetail 'Shipment line';shipment 'Shipment' = DATA Shipment (ShipmentDetail) NONULL DELETE;book 'Book' = DATA Book (ShipmentDetail);``
``shipmentDetail 'Shipment line' (Shipment s, Book b) = GROUP MAX ShipmentDetail d BY shipment(d), book(d);``