Sunday, September 16, 2012

 

Find Max Value of Multiple Columns

Nothing new, but a nice trick to find the maximum value of a number of columns within a row, due to Louis Davidson (via Simon Sabin). It uses the fact that a sub-query can reference columns from the main rowset.

select 
WorkOrderId,
(select max(DateValue)
from (select StartDate as DateValue
union all
select EndDate
union all
select DueDate
union all
select ModifiedDate) as DateCols) as MaxDate,
StartDate,
EndDate,
DueDate,
ModifiedDate
from
Production.WorkOrder





    

Powered by Blogger