Solved

Select statement that will only give me the latest (max) date where multiple records exist for each ID

Posted on 2012-12-28
6
311 Views
Last Modified: 2012-12-28
I have a table that shows multiple records per ID, it tracks the status of a loan in process and many times people will record the same status more than one time.  I'm looking for a select statement that will give me just the latest date that the loan was moved to it's current status. So in other words the max date for the max status but I can't use an aggregate function like (MAX).

for example my current select statement;
SELECT DISTINCT
                      TOP (100) PERCENT dbo.setups_loanstatus.statusdescription AS [Latest Status], dbo.loan_main.loanid, dbo.setups_loanstatus.statusorder, dbo.loan_status.statusid,
                      dbo.loan_status.datefirstentered, dbo.loan_main.lenderdatabaseid, dbo.loan_main.loanrecordid
FROM         dbo.loan_main INNER JOIN
                      dbo.setups_loanstatus ON dbo.loan_main.statusid = dbo.setups_loanstatus.statusid LEFT OUTER JOIN
                      dbo.loan_status ON dbo.setups_loanstatus.statusid = dbo.loan_status.statusid AND dbo.loan_main.lenderdatabaseid = dbo.loan_status.lenderdatabaseid AND
                      dbo.loan_main.loanrecordid = dbo.loan_status.loanrecordid
ORDER BY dbo.loan_main.loanid DESC, dbo.setups_loanstatus.statusorder DESC

gives me the correct date for the current status but if the loan has been in that status more than once I get two records for that Loan ID.
0
Comment
Question by:ISBTECH
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

by:tim_cs
ID: 38727548
Try using ROW_NUMBER()

;WITH CTE AS (
SELECT DISTINCT
                      TOP (100) PERCENT dbo.setups_loanstatus.statusdescription AS [Latest Status], dbo.loan_main.loanid, dbo.setups_loanstatus.statusorder, dbo.loan_status.statusid,
                      dbo.loan_status.datefirstentered, dbo.loan_main.lenderdatabaseid, dbo.loan_main.loanrecordid,
ROW_NUMBER() OVER (PARTITION BY LoanID, StatusID ORDER BY DateFirstEntered DESC) RN
FROM         dbo.loan_main INNER JOIN
                      dbo.setups_loanstatus ON dbo.loan_main.statusid = dbo.setups_loanstatus.statusid LEFT OUTER JOIN
                      dbo.loan_status ON dbo.setups_loanstatus.statusid = dbo.loan_status.statusid AND dbo.loan_main.lenderdatabaseid = dbo.loan_status.lenderdatabaseid AND
                      dbo.loan_main.loanrecordid = dbo.loan_status.loanrecordid)

SELECT
   *
FROM
   CTE
WHERE
   RN = 1
0
 

Author Comment

by:ISBTECH
ID: 38728203
Ok, that works but (I didn't mention this before, didn't think it matters) I'm using the select statement for a view and when I use that text in the view it crashes SQL management studio, any way it can be done to allow use in a view?
0
 
LVL 15

Expert Comment

by:tim_cs
ID: 38728209
try it without the CTE like this.

SELECT
    *
FROM
    (
     SELECT DISTINCT TOP (100) PERCENT
        dbo.setups_loanstatus.statusdescription AS [Latest Status]
       ,dbo.loan_main.loanid
       ,dbo.setups_loanstatus.statusorder
       ,dbo.loan_status.statusid
       ,dbo.loan_status.datefirstentered
       ,dbo.loan_main.lenderdatabaseid
       ,dbo.loan_main.loanrecordid
       ,ROW_NUMBER() OVER (PARTITION BY LoanID, StatusID ORDER BY DateFirstEntered DESC) RN
     FROM
        dbo.loan_main
        INNER JOIN dbo.setups_loanstatus
            ON dbo.loan_main.statusid = dbo.setups_loanstatus.statusid
        LEFT OUTER JOIN dbo.loan_status
            ON dbo.setups_loanstatus.statusid = dbo.loan_status.statusid
               AND dbo.loan_main.lenderdatabaseid = dbo.loan_status.lenderdatabaseid
               AND dbo.loan_main.loanrecordid = dbo.loan_status.loanrecordid
    ) a
WHERE
    RN = 1

Open in new window

0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:ISBTECH
ID: 38728238
Strange, still crashes management studio as a view but works fine as a query.
0
 
LVL 15

Accepted Solution

by:
tim_cs earned 500 total points
ID: 38728287
Try creating it without using the GUI.  

CREATE  VIEW [dbo].[YourViewName]
AS
    SELECT
        *
    FROM
        (
         SELECT DISTINCT TOP (100) PERCENT
            dbo.setups_loanstatus.statusdescription AS [Latest Status]
           ,dbo.loan_main.loanid
           ,dbo.setups_loanstatus.statusorder
           ,dbo.loan_status.statusid
           ,dbo.loan_status.datefirstentered
           ,dbo.loan_main.lenderdatabaseid
           ,dbo.loan_main.loanrecordid
           ,ROW_NUMBER() OVER (PARTITION BY LoanID, StatusID ORDER BY DateFirstEntered DESC) RN
         FROM
            dbo.loan_main
            INNER JOIN dbo.setups_loanstatus
                ON dbo.loan_main.statusid = dbo.setups_loanstatus.statusid
            LEFT OUTER JOIN dbo.loan_status
                ON dbo.setups_loanstatus.statusid = dbo.loan_status.statusid
                   AND dbo.loan_main.lenderdatabaseid = dbo.loan_status.lenderdatabaseid
                   AND dbo.loan_main.loanrecordid = dbo.loan_status.loanrecordid
        ) a
    WHERE
        RN = 1

GO

Open in new window

0
 

Author Closing Comment

by:ISBTECH
ID: 38728291
Yup that works, can't view it in the GUI but the data is there, Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

758 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

21 Experts available now in Live!

Get 1:1 Help Now