Muthukumar Palaniappan
Muthukumar Palaniappan

Reputation: 1670

How to select row values for max(2 columns)

I am working out a query in MS SQL Server. I have my table like this

Table( Level int, Stage int, values varchar)

Level Stage Value
1     1   
1     2  
1     3 
2     1 
2     2 

I need to find the row having maximum value by level and then by stage. ie., I need to get the result as

Level  Stage 
2      2

When I try the below query I get the value 22 and in one column. I need that in 2 distinct columns as specified above.

SELECT MAX(CAST(wfLevel as varchar(2)) + CAST(approvalStage as varchar(2)))
  FROM [AuditReporterDB].[dbo].[RequestHistory]

Can anyone help.

Upvotes: 3

Views: 412

Answers (1)

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

SELECT TOP 1 Level,Stage
FROM tableName
ORDER BY Level Desc,Stage Desc

Upvotes: 6

Related Questions