Skip to content
HPCCSystems Solutions Lab
HPCCSystems Solutions Lab

TOPN

In a sorted dataset TOPN returns the n number of records from the row 1 of the dataset.

Example


/*
TOPN Example:
*/
MyRec := RECORD
    INTEGER  ID;
    INTEGER  Income;
    STRING   LastName;
END;

MyDS := DATASET([{100, 28000, 'Sunny'}, {200, 5000, 'Jack'}, {300, 5000, 'Smith'},
                 {200, 1000, 'Danny'}, {200, 7000, 'Able'},  {100, 25000, 'Doable'},
                 {200, 1000, 'Nancy'}],
                 MyRec);


LastName := TOPN(MyDS, 5, LastName);
OUTPUT(LastName, NAMED('LastName'));

OUTPUT(TOPN(MyDS, 3, -Income), NAMED('Sorted_Income'));

Try Me

Syntax

TOPN(Dataset, Count, SortValues[,flag(s)]);
ValueDefinition
DatasetInput dataset
CountNumber of records to displat
SortValuesFields that sorting will be executed on
flagsOptional

Flags

OptionsDescription
UNORDEREDSpecifies the output record order is not significant
ORDEREDSpecifies the significance of the output record order
STABLESpecifies the input record order is significant
PARALLELTry to evaluate this activity in parallel
LOCALSpecifies the operation is performed on each node independently, without requiring interaction with all other nodes to acquire data
GROUPSpecifies the recordset is GROUPed and the ROLLUP operation will produce a single output record for each group