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
316 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
[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
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

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