<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MAX &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/max/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Tue, 19 Sep 2017 07:14:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Find max value from multiple columns</title>
		<link>https://www.dbrnd.com/2017/08/sql-puzzle-sql-advance-query-find-max-value-from-multiple-columns/</link>
					<comments>https://www.dbrnd.com/2017/08/sql-puzzle-sql-advance-query-find-max-value-from-multiple-columns/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 05 Aug 2017 16:16:20 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[MAX]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5757</guid>

					<description><![CDATA[Check the below input data and require output data to find max value from multiple columns. Input Data: [crayon-6ab89a1d86bf1024427534/] Require output data: [crayon-6ab89a1d86bf4572284796/] Create a table with sample data: [crayon-6ab89a1d86bf5019423158/] Solution 1: Use CASE .. WHEN [crayon-6ab89a1d86bf6707816012/] Solution 2: Use Subquery and MAX() [crayon-6ab89a1d86bf7253062701/] Solution 3: Use MAX() with Group BY [crayon-6ab89a1d86bf8325559516/] Please try the [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F08%2Fsql-puzzle-sql-advance-query-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20max%20value%20from%20multiple%20columns" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F08%2Fsql-puzzle-sql-advance-query-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20max%20value%20from%20multiple%20columns" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F08%2Fsql-puzzle-sql-advance-query-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20max%20value%20from%20multiple%20columns" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F08%2Fsql-puzzle-sql-advance-query-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20max%20value%20from%20multiple%20columns" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F08%2Fsql-puzzle-sql-advance-query-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20max%20value%20from%20multiple%20columns" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F08%2Fsql-puzzle-sql-advance-query-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20max%20value%20from%20multiple%20columns" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F08%2Fsql-puzzle-sql-advance-query-find-max-value-from-multiple-columns%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20max%20value%20from%20multiple%20columns" data-a2a-url="https://www.dbrnd.com/2017/08/sql-puzzle-sql-advance-query-find-max-value-from-multiple-columns/" data-a2a-title="SQL Puzzle: SQL Advance Query – Find max value from multiple columns"></a></p><p>Check the below input data and require output data to find max value from multiple columns.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">Class      Mark1       Mark2       Mark3
---------- ----------- ----------- -----------
A          88          98          52
B          85          54          93
C          65          87          45
D          35          77          63</pre> </p>
<p><strong>Require output data:</strong></p>
<pre class="crayon-plain-tag">Class      MaxMarks
---------- -----------
A          98
B          93
C          87
D          77</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE MaxClassMarks
(
	Class VARCHAR(10)
	,Mark1 INT
	,Mark2 INT
	,Mark3 INT
)
GO

INSERT INTO MaxClassMarks 
VALUES
 ('A',88,98,52)
,('B',85,54,93)
,('C',65,87,45)
,('D',35,77,63)
GO</pre> </p>
<p><strong>Solution 1: Use CASE .. WHEN</strong></p>
<pre class="crayon-plain-tag">SELECT Class,
       CASE WHEN mark1 &gt; mark2 AND mark1 &gt; mark3 THEN mark1
       WHEN mark2 &gt; mark1 AND mark2 &gt; mark3 THEN mark2
       WHEN mark3 &gt; mark2 AND mark3 &gt; mark1 THEN mark3
       END AS MaxMarks
FROM MaxClassMarks</pre> </p>
<p><strong>Solution 2: Use Subquery and MAX()</strong></p>
<pre class="crayon-plain-tag">SELECT Class,
(
       SELECT Max(T)
       FROM (
              VALUES (mark1) , (mark2), (mark3) 
       ) as value(T)
) as maxmark
FROM MaxClassMarks</pre> </p>
<p><strong>Solution 3: Use MAX() with Group BY</strong></p>
<pre class="crayon-plain-tag">SELECT Class, MAX(maxmark) maxmark
FROM
(
       SELECT Class , mark1 as maxmark FROM MaxClassMarks
       UNION ALL
       SELECT Class , mark2 as maxmark FROM MaxClassMarks
       UNION ALl
       SELECT Class , mark3 as maxmark FROM MaxClassMarks
) a GROUP BY Class</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/08/sql-puzzle-sql-advance-query-find-max-value-from-multiple-columns/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Find MAX value from Multiple Columns</title>
		<link>https://www.dbrnd.com/2015/12/sql-server-find-max-value-from-multiple-columns/</link>
					<comments>https://www.dbrnd.com/2015/12/sql-server-find-max-value-from-multiple-columns/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 27 Dec 2015 15:21:17 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[MAX]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1159</guid>

					<description><![CDATA[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. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F12%2Fsql-server-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Server%3A%20Find%20MAX%20value%20from%20Multiple%20Columns" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F12%2Fsql-server-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Server%3A%20Find%20MAX%20value%20from%20Multiple%20Columns" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F12%2Fsql-server-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Server%3A%20Find%20MAX%20value%20from%20Multiple%20Columns" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F12%2Fsql-server-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Server%3A%20Find%20MAX%20value%20from%20Multiple%20Columns" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F12%2Fsql-server-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Server%3A%20Find%20MAX%20value%20from%20Multiple%20Columns" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F12%2Fsql-server-find-max-value-from-multiple-columns%2F&amp;linkname=SQL%20Server%3A%20Find%20MAX%20value%20from%20Multiple%20Columns" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F12%2Fsql-server-find-max-value-from-multiple-columns%2F&#038;title=SQL%20Server%3A%20Find%20MAX%20value%20from%20Multiple%20Columns" data-a2a-url="https://www.dbrnd.com/2015/12/sql-server-find-max-value-from-multiple-columns/" data-a2a-title="SQL Server: Find MAX value from Multiple Columns"></a></p><p>In this post, I am sharing a T-SQL Script to find the MAX value from Multiple columns in SQL Server.</p>
<p>For example:<br />
In our table, we have three different date columns and need to find the one max value out of this three columns.</p>
<p>I worked around this and tested different solution for the performance. I am sharing one of the best solutions here.</p>
<p>I have tested different solutions like UNPIVOT, UNION, InnerQuery.</p>
<p>Note: Below T-SQL script solution work with the only same type of columns.</p>
<p><strong>Let&#8217;s First, create sample table and data:</strong></p><pre class="crayon-plain-tag">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')</pre><p>
<p><strong>T-SQL Script to Find MAX Value from three different Date Columns:</strong></p><pre class="crayon-plain-tag">SELECT 
   ID, 
   (SELECT MAX(LastUpdateDate)
      FROM (VALUES (UpdatedDate1),(UpdatedDate2),(UpdatedDate3)) AS UpdateDate(LastUpdateDate)) 
   AS LastUpdateDate
FROM tbl_FindMAX</pre><p>
<p><strong>The Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/12/MaxValueFromMultiple.png"><img decoding="async" class="alignnone wp-image-1163 size-full" title="Max Value From Multiple Columns SQL Server" src="https://www.dbrnd.com/wp-content/uploads/2015/12/MaxValueFromMultiple.png" alt="Max Value From Multiple Columns SQL Server" width="217" height="94" srcset="https://www.dbrnd.com/wp-content/uploads/2015/12/MaxValueFromMultiple.png 217w, https://www.dbrnd.com/wp-content/uploads/2015/12/MaxValueFromMultiple-150x65.png 150w" sizes="(max-width: 217px) 100vw, 217px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/12/sql-server-find-max-value-from-multiple-columns/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
