Billy
Billy

Reputation: 15706

Get the maximum of 2 columns in SQL Server

If i have a sql table with column like this
id version subversion
1 1 0
1 1 2
1 2 0
1 2 1

I want to get the latest version, in this case is 2.1.

What should I do?

Upvotes: 0

Views: 113

Answers (2)

Matthew Scharley
Matthew Scharley

Reputation: 132464

SELECT TOP 1 * FROM [Versions] ORDER BY [version] DESC, [subversion] DESC

should work fine... It works in MySQL atleast, and this is the basic MSSQL translation.

For reference, since the edit history isn't shown yet, my original query was:

SELECT * FROM [Versions] ORDER BY [version] DESC, [subversion] DESC LIMIT 1

Apparently MSSQL doesn't have the limit clause though, only some workarounds.

Upvotes: 5

Abtin Forouzandeh
Abtin Forouzandeh

Reputation: 5855

SELECT TOP 1 * FROM table ORDER BY version DESC, subversion DESC

Upvotes: 1

Related Questions