ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Query to select latest Record from specific Group in SQL Server <p>Hi&nbsp;smile,</p> <p>Refer the below sample Query.</p> <p><strong><span style="text-decoration: underline;">SQL</span></strong></p> <pre class="brush: sql">DECLARE @TestDemo AS TABLE(AdmissionNo VARCHAR(10), [Month] VARCHAR(10), Recievable INT, NetBal INT, Paid INT, RemBal INT) INSERT INTO @TestDemo VALUES(&#39;R-01&#39;,&#39;Jan&#39;,1000,1000,600,400) INSERT INTO @TestDemo VALUES(&#39;R-01&#39;,&#39;Jan&#39;,1000,400,200,100) INSERT INTO @TestDemo VALUES(&#39;R-02&#39;,&#39;Jan&#39;,1000,1000,400,600) INSERT INTO @TestDemo VALUES(&#39;R-02&#39;,&#39;Jan&#39;,1000,600,300,300) INSERT INTO @TestDemo VALUES(&#39;R-02&#39;,&#39;Jan&#39;,1000,300,200,100) INSERT INTO @TestDemo VALUES(&#39;R-03&#39;,&#39;Jan&#39;,1000,1000,500,500) INSERT INTO @TestDemo VALUES(&#39;R-03&#39;,&#39;Jan&#39;,1000,500,200,300) INSERT INTO @TestDemo VALUES(&#39;R-03&#39;,&#39;Jan&#39;,1000,300,150,150) INSERT INTO @TestDemo VALUES(&#39;R-04&#39;,&#39;Jan&#39;,1000,1000,700,300) INSERT INTO @TestDemo VALUES(&#39;R-05&#39;,&#39;Jan&#39;,1000,1000,800,200) ;WITH Employee_CTE (RowNo,AdmissionNo,Month,Recievable,NetBal, Paid,RemBal) AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 0)) RowNo,AdmissionNo,Month,Recievable,NetBal,Paid,RemBal FROM @TestDemo) SELECT AdmissionNo,Month,Recievable,NetBal,Paid,RemBal FROM Employee_CTE WHERE RowNo IN (SELECT MAX(RowNo) FROM Employee_CTE GROUP BY AdmissionNo)</pre> <p><strong><span style="text-decoration: underline;">Output</span></strong></p> <table border="1"> <tbody> <tr> <td>AdmissionNo</td> <td>Month</td> <td>Recievable</td> <td>NetBal</td> <td>Paid</td> <td>RemBal</td> </tr> <tr> <td>R-01</td> <td>Jan</td> <td>1000</td> <td>400</td> <td>200</td> <td>100</td> </tr> <tr> <td>R-02</td> <td>Jan</td> <td>1000</td> <td>300</td> <td>200</td> <td>100</td> </tr> <tr> <td>R-03</td> <td>Jan</td> <td>1000</td> <td>300</td> <td>150</td> <td>150</td> </tr> <tr> <td>R-04</td> <td>Jan</td> <td>1000</td> <td>1000</td> <td>700</td> <td>300</td> </tr> <tr> <td>R-05</td> <td>Jan</td> <td>1000</td> <td>1000</td> <td>800</td> <td>200</td> </tr> </tbody> </table> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/206310/Query-to-select-latest-Record-from-specific-Group-in-SQL-Server/https://www.aspforums.net:443/Threads/206310/Query-to-select-latest-Record-from-specific-Group-in-SQL-Server/Fri, 23 Nov 2018 05:19:53 GMT