Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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.4k 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
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...