这感觉应该是一件容易的事。如何获取不同列中 3 个日期中的最新日期
DROP TABLE #dates
CREATE TABLE #dates (mykey CHAR(10), date1 DATETIME, date2 DATETIME, date3 DATETIME)
INSERT #dates VALUES ('Key1', '1/1/2015', '2/1/2015', '3/1/2105')
INSERT #dates VALUES ('Key2', '1/2/2015', '4/2/2015', '3/2/2105')
INSERT #dates VALUES ('Key3', '1/3/2016', '4/3/2015', '3/3/2105')
select mykey, ?? AS 'Latest Date' from #dates
我希望结果是:
mykey Latest Date
Key1 2105-03-01 00:00:00.000
Key2 2015-04-02 00:00:00.000
Key3 2016-01-03 00:00:00.000
最佳答案
SQL Server 中最简单的方法可能是使用交叉应用
:
select d.*, m.maxdate
from #dates d cross apply
(select max(dte) as maxdate
from (values (date1), (date2), (date3)) as v(dte)
) m;
关于sql - 在SQL Server中,如何获取多列日期的最新日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34404800/