Solved

MS SQL 2005 Top 1 per account on history table

Posted on 2011-09-09
5
256 Views
Last Modified: 2012-05-12
Need a sql query that will pull one record per AccNum based on the latest DateRec.
So the table lis like this

SO(ID), AccNum, Status, DateRec
1111,29,Closed,9/1/2011,120
1110,29,Closed,8/30/2011,100
1109.31,Closed,8/30/2011,100
1108,32,Closed,8/30/2011,50
1107,32,Closed,8/29/2011,50
1105,32,Closed,8/28/2011,50

Again, What I want to end up with is a just one record per Accnum, based on the latest DateRec

For this example I would end up with 3 rows
SO(ID), AccNum, Status, DateRec, Amount
1111,29,Closed,9/1/2011,120
1109.31,Closed,8/30/2011,100
1108,32,Closed,8/30/2011,50
0
Comment
Question by:matrixnetworks
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 36512219
With Acct_Detail as
(
select
            SO
      ,      AccNum
      ,      Status
      ,      DateRec
      ,      row_number() over (Partition By AccNUm Order by DateRec desc) as Instance_Counter
)

select      *
from      Acct_Detail
where Instance_Counter = 1
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 36512226
Note:  The "row_number() over (... line will assign a unique number starting with 1 based on each record that has the same AccNum.  It is ordered by DateRec in descending order so each "Instance_Counter" with a "1", represents the most recent AccNum by date.
0
 

Author Comment

by:matrixnetworks
ID: 36512285
Where do I put the table that I am pulling the Acct_Details from?
Here is the real query info, but it's throwing an error,

WITH Acct_Details AS (SELECT     SONumber, AccountNumber, Status, DateReceived, row_number() over (Partition by AccountNumber Order by DateReceieved DESC) as Instance_Counter)
                                                 FROM          dbo.tblServiceOrders)
    SELECT    *
     FROM         Acct_Details
where Instance_Counter = 1
0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 36512299
Sorry about that.  Typo.

WITH Acct_Details AS
(SELECT    
            SONumber
      ,      AccountNumber
      ,      Status
      ,      DateReceived
      ,      row_number() over (Partition by AccountNumber Order by DateReceieved DESC) as Instance_Counter

      FROM          dbo.tblServiceOrders
)

SELECT    *
FROM         Acct_Details
where Instance_Counter = 1
0
 

Author Closing Comment

by:matrixnetworks
ID: 36512474
Thank you very much for your help!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now