如何为max(2列)选择行值

时间:2022-09-26 21:03:21

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

我正在使用MS SQL Server进行查询。我的桌子是这样的

Table( Level int, Stage int, values varchar)

表(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

我需要找到一个有最大值的行,然后是阶段性的。ie。,我需要得到如下结果

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.

当我尝试下面的查询时,我得到值22,在一列中。我需要两个不同的列,如上所述。

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

Can anyone help.

谁能帮助。

1 个解决方案

#1


6  

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

#1


6  

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