Sqlserver
 SQL >> Datenbank >  >> RDS >> Sqlserver

SQL MAX über mehrere Spalten hinweg

Hier ist eine weitere elegante Möglichkeit, um in T-SQL auf SQL Server ein Max-Verhalten über mehrere Spalten hinweg zu erreichen:

SELECT [Other Fields], (SELECT Max(v) FROM (VALUES (date1), (date2), (date3),...) AS value(v)) as [MaxDate] FROM [YourTableName] 

Die entscheidende Zutat ist der Tabellenwert-Konstruktor (Table Value Constructor). In der Dokumentation heißt es dazu:

"Specifies a set of row value expressions to be constructed into a table. The Transact-SQL table value constructor allows multiple rows of data to be specified in a single DML statement. The table value constructor can be specified either as the VALUES clause of an INSERT ... VALUES statement, or as a derived table in either the USING clause of the MERGE statement or the FROM clause."