Back
How do you return 1 value per row of the max of several columns:
TableName
[Number, Date1, Date2, Date3, Cost]
I need to return something like this:
[Number, Most_Recent_Date, Cost]
Query?
You can try using the CASE statement as mentioned below:
SELECT CASE WHEN DateA>= DateB AND DateA>= DateC THEN DateA WHEN DateB>= DateA AND DateB>= DateC THEN DateB WHEN DateC>= DateA AND DateC>= DateB THEN DateC ELSE DateA END AS Most_Recent_Date
SELECT
CASE
WHEN DateA>= DateB AND DateA>= DateC THEN DateA
WHEN DateB>= DateA AND DateB>= DateC THEN DateB
WHEN DateC>= DateA AND DateC>= DateB THEN DateC
ELSE DateA
END AS Most_Recent_Date
31k questions
32.8k answers
501 comments
693 users