ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Query to select Sum of 3 Rows having maximum values in SQL Server<p>Hi <a class="username" rel="iammann"> iammann</a>,</p> <p>Please refer below query.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: sql">DECLARE @testdemo AS TABLE(id int, [1] int ,[2] int, [3] int ,[4] int) INSERT INTO @testdemo VALUES(1,21,31,43,13) INSERT INTO @testdemo VALUES(2,12,22,25,23) INSERT INTO @testdemo VALUES(3,51,44,24,55) INSERT INTO @testdemo VALUES(4,10,04,30,85) SELECT SUM(T1.[1])[1],SUM(T2.[2])[2],SUM(T3.[3])[3] FROM (SELECT TOP 3 ROW_NUMBER() OVER (ORDER BY ([1]) DESC)row,[1] FROM @testdemo) AS T1 INNER JOIN (SELECT TOP 3 ROW_NUMBER() OVER (ORDER BY ([2]) DESC)row,[2] FROM @testdemo ) AS T2 ON T1.row = T2.row INNER JOIN (SELECT TOP 3 ROW_NUMBER() OVER (ORDER BY ([3]) DESC)row,[3] FROM @testdemo) AS T3 ON T1.row = T3.row</pre> <p><span style="text-decoration: underline;"><strong>Output</strong></span></p> <table border="1"> <tbody> <tr> <td><strong>1</strong></td> <td><strong>2</strong></td> <td><strong>3</strong></td> </tr> <tr> <td>84</td> <td>97</td> <td>98</td> </tr> </tbody> </table>, 14 Nov 2018 02:21:10 GMT