I am creating a version control domain class in Grails:
class VersionControl {
Date dateCreated
Long versionNumber
Long getLatestVersionNumber() {
//return largest versionNumber
}
}
I would like to add a query to get the largest version number stored:
Long getLatestVersionNumber()
In SQL this query would look more or less as follows:
SELECT TOP 1 MAX(versionNumber) FROM VersionControl
The function MUST return the value as a long.
What is the correct way to do this in Grails?
The SQL MIN() and MAX() Functions The MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.
The MAX() function can be used on the string column. For example, the following uses the MAX() function on the LastName column of the Employee table. It will sort the column alphabetically and the last value will be returned.
5. What is the maximum value in the price column of the car_info table? Correct: To ensure that the values in the price column fell within the expected range, you used the MIN and MAX functions to determine that the maximum price was 45, 400.
You can use GORM criteria, querying with projections:
VersionControl.createCriteria().get {
projections {
max "versionNumber"
}
} as Long
VersionControl.executeQuery("select max(versionNumber) from VersionControl")
This is easier for me to understand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With