To get the Maximum on some field without where clasue

WITH cte AS (
   SELECT home, MAX(year) AS year FROM Table1 GROUP BY home
)
SELECT * FROM Table1 a INNER JOIN cte ON a.home = cte.home AND a.year = cte.year


Example:
        WITH    cte
                  AS ( SELECT   MasterLoanLinksID ,
                                MAX(Version) AS VersionNumber
                       FROM     @L
                       GROUP BY MasterLoanLinksID
                     )
            INSERT  INTO @LFinal
                    ( LFLoanLinksId ,
                      LFMasterLoanLinksID
                    )
                    SELECT  a.LoanLinksId ,
                            a.MasterLoanLinksID
                    FROM    LoanBorrowerLinks a
                            INNER JOIN cte ON a.MasterLoanLinksID = cte.MasterLoanLinksID
                                              AND a.VersionNumber = cte.VersionNumber
                    WHERE   a.IsActive = 1
                            AND IsPublished = 0;

Comments

Popular posts from this blog

TSQL To Get All the Names of tables and Databases whose data have changed in last 24 Hours

To Get All the Names of tables and Databases their data have changed in last 24 Hours And Backup those databases

Apply paging in procedure