Getting the last record in SQL in WHERE condition

后端 未结 9 2259
借酒劲吻你
借酒劲吻你 2021-02-15 04:24

i have loanTable that contain two field loan_id and status

loan_id status
==============
1       0
2       9
1       6
         


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-15 04:54

    I think this code may help you:

    WITH cte_Loans
    AS
    (
    SELECT   LoanID
            ,[Status]
            ,ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS RN
    FROM    LoanTable
    )
    
    SELECT   LoanID
            ,[Status]
    FROM    LoanTable L1
    WHERE   RN = (  SELECT max(RN)
                    FROM LoanTable L2
                    WHERE L2.LoanID = L1.LoanID)
    

提交回复
热议问题