sql 怎么查询一个列的最大值

2024-11-08 21:51:27
推荐回答(2个)
回答(1):

方法一

SELECT ID ,
Name ,
( SELECT MAX(LastUpdateDate)
FROM ( VALUES ( UpdateByApp1Date), ( UpdateByApp2Date),
( UpdateByApp3Date) ) AS UpdateDate ( LastUpdateDate )
) AS LastUpdateDate
FROM ##TestTable

方法二

SELECT ID ,
[Name] ,
MAX(UpdateDate) AS LastUpdateDate
FROM ##TestTable UNPIVOT ( UpdateDate FOR DateVal IN ( UpdateByApp1Date,
UpdateByApp2Date,
UpdateByApp3Date ) ) AS u
GROUP BY ID ,
Name

方法三

SELECT ID ,
name ,
( SELECT MAX(UpdateDate) AS LastUpdateDate
FROM ( SELECT tt.UpdateByApp1Date AS UpdateDate
UNION
SELECT tt.UpdateByApp2Date
UNION
SELECT tt.UpdateByApp3Date
) ud
) LastUpdateDate
FROM ##TestTable tt

回答(2):

select max(column) from tablename