Welcome to MSSQL Forum!
MSSQL Forum - Discussions about Microsoft SQL Server

You are currently viewing our community forums as a guest user. Sign up or
Having an account grants you additional privileges, such as creating and participating in discussions.

Query to get last record change per account number

Discussion in 'SQL Server Scripts and Tools' started by Far, Jul 13, 2017.

  1. Far

    Far New Member

    Hello Everyone,
    I needed a little help!
    What i wanted to achieve is that, from a table i have several data per account number, -i.e there are several date the data has changed, i wanted to get only the latest data per account number by its last date the account has been updated.
    E.g
    upload_2017-7-13_11-6-47.png

    acc no on row 133 and 134 is the same, but date prop has been changed, i want to get only the data for the last record only for each account number.

    Here is my actual query used.

    Code (text):
    SELECT distinct e.acctno
            , r.[custid]
            , c.title
            ,c.firstname
            ,c.name
            ,r.[empeeno]
            ,r.[dateprop]
            ,r.[reflresult]
            ,r.[datereferral]

        FROM cosacs.dbo.customer as c
            ,[cosacs].[dbo].[referral] as r
            ,cosacs.dbo.custacct as e
            ,cosacs.dbo.acct as a

        where  e.custid = r.custid
            and c.custid = r.custid
            and e.acctno = a.acctno
            and a.accttype != 'C'
            and a.accttype != 'S'

      ORDER BY r.[dateprop] DESC
    Last edited: Jul 13, 2017

Share This Page