Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS SQL 2005 Top 1 per account on history table

Posted on 2011-09-09
5
Medium Priority
?
294 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

715 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