In this post, I am sharing one T-SQL Script to find MAX value from Multiple columns in SQL Server.
At our table, we have three different updated date columns and require to find the one max value out of this three column.
I have worked around this and tested different solution for performance aspect also because I executed this script over the 10000000 production data and out this all different solutions, I am sharing one of the best solution here.
I have tested different solutions like, UNPIVOT, UNION, InnerQuery.
This T-SQL script solution work with only same type of columns.
Let’s First, create sample table and data:
CREATE TABLE tbl_FindMAX
ID INT IDENTITY(1,1) PRIMARY KEY
INSERT INTO tbl_FindMAX(Name, UpdatedDate1, UpdatedDate2, UpdatedDate3 )
VALUES('Anvesh', '2015-08-08','2015-10-10', '2015-11-08'),
('Neevan', '2015-08-26','2010-10-08', '2015-05-16'),
('Akash', '2014-02-26','2014-02-16', '2015-08-16')
T-SQL Script to Find MAX Value from three different Date Columns:
FROM (VALUES (UpdatedDate1),(UpdatedDate2),(UpdatedDate3)) AS UpdateDate(LastUpdateDate))
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.