Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 December SQL Server: Find MAX value from Multiple Columns

SQL Server: Find MAX value from Multiple Columns

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a T-SQL Script to find the MAX value from Multiple columns in SQL Server.

For example:
In our table, we have three different date columns and need to find the one max value out of this three columns.

I worked around this and tested different solution for the performance. I am sharing one of the best solutions here.

I have tested different solutions like UNPIVOT, UNION, InnerQuery.

Note: Below T-SQL script solution work with the only same type of columns.

Let’s First, create sample table and data:

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE tbl_FindMAX
(
ID INT IDENTITY(1,1) PRIMARY KEY
,Name NVARCHAR(255)
,UpdatedDate1 DATETIME
,UpdatedDate2 DATETIME
,UpdatedDate3 DATETIME
)
 
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:

1
2
3
4
5
6
SELECT
ID,
(SELECT MAX(LastUpdateDate)
FROM (VALUES (UpdatedDate1),(UpdatedDate2),(UpdatedDate3)) AS UpdateDate(LastUpdateDate))
AS LastUpdateDate
FROM tbl_FindMAX

The Result:

Max Value From Multiple Columns SQL Server

Dec 27, 2015Anvesh Patel
SQL Server: Concatenate Strings using the GROUP BY clauseSQL Server: How to Convert Varbinary to Numeric & vice versa?
Comments: 1
  1. Gavin
    February 21, 2016 at 7:30 am

    Great post.

Anvesh Patel
Anvesh Patel

Database Engineer

December 27, 2015 SQL ServerAnvesh Patel, database, database research and development, dbrnd, MAX, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....