Solved

MS SQL 2005 Top 1 per account on history table

Posted on 2011-09-09
5
247 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much for your help!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# SQL BULK INSERT CLASS 5 33
Scheduling Jobs for Execution: 4 13
SQL Transaction logs 8 6
Azure SQL DB? 3 13
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

772 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

12 Experts available now in Live!

Get 1:1 Help Now