Solved

MS SQL 2005 Top 1 per account on history table

Posted on 2011-09-09
5
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

740 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