0

Selecting top ranking record

Hello team,

Here's my table

I'm looking to select the make that has sold the most cars altogether eg in this case volvo with 120 sales. 

I've tried

select(@make,groupby(@make,@sales)=MAX(groupby(@make,@sales))) 

but it doesn't return the correct make (not sure what logic is at work here).

 

Anyone has a better way of doing this ?

 

Thanks !

1 comment

  • Avatar
    Parker Selman Official comment

    Hi Lauren, 


    Thanks for the post! 

    When using a SELECT formula, the amount of records in your first term (ie the data you'd like to select) and your second term (the condition) must be equal, otherwise calculation errors can occur. In your case, you're listing the full @make column as the first term, which contains 6 items excluding headers. Then, in your second term, you're grouping the makes, returning only 3 values. 

    To correct this, I'd recommend wrapping your first term in a GROUP function like so:

    SELECT(GROUP(@make),GROUPBY(@make,@sales)=MAX(GROUPBY(@make,@sales))) 

    This should return the values you're looking for! 

    Best,

    Parker

Please sign in to leave a comment.