Ranking from a .MAX aggregate

Hi,

when doing this https://www.wix.com/code/reference/wix-data.WixDataAggregate.html#max

I want a field in my table that will diplay the rank. I have sorted out the result from higher to lower, so I want the higher to have rank 1, to lower let say to 10 or more…

Is there a way to do it ?

Hi Yanick!

Sure there is.

For example, lets say we want to select top ranking for each movie. We have collection Movies, with title serving as movie title and rank as the rank.

wixData.aggregate('Movies')
  .group('title')
  .max('rank', 'maxRank')
  .descending('maxRank')
  .run()

This will sort by max rank in descending order.

I hope this helps.

hi,

Thanks for the answer, but this is already done.

What I want is the 1st should be #1 and second in table #2, etc.

You can have a look at that page https://tinetine11.wixsite.com/website/principalcompetitif/Sans-Limites-Dames-moins-de-10-ans

But I want a field that will have a number 1 to xx depending of the rank of the competitor best results. This is a dynamic page coming from the Home page depending on the category.

The best score will change during the year so their ranking as well.

@yranzi if you want to display the ranking number that would have to be done in code.

Since you know that the first element is #1, second is #2, etc. you can just append the number yourself. Something like this would do:

const aggregateResult = wixData.aggregate...;
const rankedItems = aggregateResult.map(
    (item, index) => Object.assign({}, item, { rank: index+1 })
  );

If it is not the first page, you would also add the page offset.

Hi,

thanks again for the answer :wink: But not sure to understand what I have to do. Here is the code behind that I’ve made (I’ve added what you put in there, but seems to have an error) :

// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixData from ‘wix-data’
$w.onReady( function () {
let donCategorie = $w(‘#dynamicDataset’).getCurrentItem()
let filter = wixData.filter().eq(“categorie”, donCategorie.categorie);
$w(‘#text7’).text = donCategorie.categorie

const aggregateResult = wixData.aggregate(“principalcompetitif”);
const rankedItems = aggregateResult.map ( (item, index) => Object.assign({}, item, { rank: index+1 }) );

wixData.aggregate("principalcompetitif") 

    .filter(filter) 
    .group("prenom", "nom", "cpa") 
    .max("short") 
    .descending("shortMax") 
    .limit(1000) 
    .run() 
    .then(res => { 

//console.log(res.items);
$w(‘#table2’).rows = res.items;
});

//console.log(donCategorie.categorie);
});

The idea is to make your then statement into:

$w(‘#table2’).rows = res.items.map((item, index) => Object.assign({}, item, { rank: index + 1 }));

This will add rank attribute to every item. Rank will hold the position.

Wow thank you, its working. Sorry for the delayed response, I didn’t receive the email that I had an answer :slight_smile: and just checked to be sure nobody response, and there was the solution :slight_smile:

Thank you very much again.