Category: How do I do X?
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

Query - Select the largest number in DB Column

Issue

I am new to using AppSmith and am having trouble finding examples or answers to my questions. I need to retrieve the largest number from a database column, and use that number to create a unique and sequential ID for a new member in the database. I have tried using the MAX SQL function, but need more guidance on how to incorporate it into my solution.

Resolution

The user is trying to select and retrieve the largest number in a database column and use it to generate a unique sequential ID for a new member they are adding to the database. To accomplish this, they can use the MAX SQL function to get the largest number in the column. For example, the SELECT MAX(SALARY) FROM Employee; will return the largest salary in the Employee table.

To generate a unique sequential ID, they can create a function that retrieves the data from the database, maps it to the column they want to use, and then uses the Math.max() function to get the largest number. They can then add 1 to this number to get the new sequential ID.

Here is an example code snippet that demonstrates this solution:

{{
function(){
var data = get_clientlastid.data.map(o => o.client_id);
var lastid = Math.max(...data);
var newid = lastid + 1;
return newid;
}
()
}}

This code retrieves the data from the get_clientlastid function (which should be replaced with the appropriate function name) and maps it to the client_id column. It then uses the Math.max() function to get the largest number in the column, adds 1 to it, and returns the new sequential ID.