How to limit the result of groupby accum

Hi,
Capture

This is my query…I want count in descending order and also limit the result like top3

Thank you

So for this specific example, you dont actually need a group by accum - but we can look at a few variations

  1. just use a MapAccum - this prints out all the cities and their counts
MapAccum<STRING, INT> @@cityCount:

res = SELECT tgt FROM start:s -(reverse_edge_type_1) - Person:tgt 
        ACCUM @@cityCount += (tgt.City ->1);
print @@cityCount;
  1. HeapAccum to limit the size of the output, where lim is an input INT argument
TYPEDEF tuple<STRING cityName,  INT cnt> cityCnts;

HeapAccum<cityCnts>(lim, cnt DESC) @@mainCityCount;
MapAccum<STRING, INT> @@cityCount:

res = SELECT tgt FROM start:s -(reverse_edge_type_1) - Person:tgt 
        ACCUM @@cityCount += (tgt.City ->1)
        POST-ACCUM 
            FOREACH (x,y) IN @@cityCount
                    @@mainCityCount +=  cityCnts(x,y)
            END
print @@mainCityCount;
4 Likes