Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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?

1 Answer

0 votes
by (40.7k points)

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

Related questions

0 votes
1 answer
asked Jul 25, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
+1 vote
1 answer

Browse Categories

...