永发信息网

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

答案:2  悬赏:60  手机版
解决时间 2021-03-03 12:01
  • 提问者网友:一抹荒凉废墟
  • 2021-03-03 05:13
sql 怎么查询一个列的最大值
最佳答案
  • 五星知识达人网友:想偏头吻你
  • 2021-03-03 06:50
方法一

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
全部回答
  • 1楼网友:鱼忧
  • 2021-03-03 07:09
select a.id from table a left outer join ( select name, max(num) max_num from table group by name ) b on a.name = b.name and a.num = b.max_num
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯