Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 325901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:15:35+00:00 2026-05-12T09:15:35+00:00

Thanks to a previous question, I found out how to pull the most recent

  • 0

Thanks to a previous question, I found out how to pull the most recent data based on a linked table. BUT, now I have a related question.

The solution that I found used row_number() and PARTITION to pull the most recent set of data. But what if there’s a possibility for zero or more rows in a linked table in the view? For example, the table FollowUpDate might have 0 rows, or 1, or more. I just want the most recent FollowUpDate:

SELECT 
    EFD.FormId
    ,EFD.StatusName
    ,MAX(EFD.ActionDate)
    ,EFT.Name AS FormType
    ,ECOA.Account AS ChargeOffAccount
    ,ERC.Name AS ReasonCode
    ,EAC.Description AS ApprovalCode
    ,MAX(EFU.FollowUpDate) AS FollowUpDate
FROM (
  SELECT EF.FormId, EFD.ActionDate, EFS.Name AS StatusName, EF.FormTypeId, EF.ChargeOffId, EF.ReasonCodeId, EF.ApprovalCodeId, 
         row_number() OVER ( PARTITION BY EF.FormId ORDER BY EFD.ActionDate DESC ) DateSortKey
    FROM Extension.FormDate EFD INNER JOIN Extension.Form EF ON EFD.FormId = EF.FormId INNER JOIN Extension.FormStatus EFS ON EFD.StatusId = EFS.StatusId
  ) EFD
    INNER JOIN Extension.FormType EFT ON EFD.FormTypeId = EFT.FormTypeId
    LEFT OUTER JOIN Extension.ChargeOffAccount ECOA ON EFD.ChargeOffId = ECOA.ChargeOffId
    LEFT OUTER JOIN Extension.ReasonCode ERC ON EFD.ReasonCodeId = ERC.ReasonCodeId
    LEFT OUTER JOIN Extension.ApprovalCode EAC ON EFD.ApprovalCodeId = EAC.ApprovalCodeId
    LEFT OUTER JOIN (Select EFU.FormId, EFU.FollowUpDate, row_number() OVER (PARTITION BY EFU.FormId ORDER BY EFU.FollowUpDate DESC) FUDateSortKey FROM Extension.FormFollowUp EFU INNER JOIN Extension.Form EF ON EFU.FormId = EF.FormId) EFU ON EFD.FormId = EFU.FormId
WHERE EFD.DateSortKey = 1
GROUP BY
    EFD.FormId, EFD.ActionDate, EFD.StatusName, EFT.Name, ECOA.Account, ERC.Name, EAC.Description, EFU.FollowUpDate
ORDER BY 
    EFD.FormId 

If I do a similar pull using row_number() and PARTITION, I get the data only if there is at least one row in FollowUpDate. Kinda defeats the purpose of a LEFT OUTER JOIN. Can anyone help me get this working?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:15:35+00:00Added an answer on May 12, 2026 at 9:15 am

    I rewrote your query – you had unnecessary subselects, and used row_number() for the FUDateSortKey but didn’t use the column:

    SELECT t.formid,
       t.statusname,
       MAX(t.actiondate) 'actiondate',
       t.formtype,
       t.chargeoffaccount,
       t.reasoncode,
       t.approvalcode,
       MAX(t.followupdate) 'followupdate'
    FROM (
       SELECT t.formid, 
              fs.name 'StatusName',
              t.actiondate,
              ft.name 'formtype',
              coa.account 'ChargeOffAccount',
              rc.name 'ReasonCode',
              ac.description 'ApprovalCode',
              ffu.followupdate,
              row_number() OVER (PARTITION BY ef.formid ORDER BY t.actiondate DESC) 'DateSortKey'
         FROM EXTENSION.FORMDATE t 
         JOIN EXTENSION.FORM ef ON ef.formid = t.formid
         JOIN EXTENSION.FORMSTATUS fs ON fs.statusid = t.statusid
         JOIN EXTENSION.FORMTYPE ft ON ft.formtypeid = ef.formtypeid
    LEFT JOIN EXTENSION.CHARGEOFFACCOUNT coa ON coa.chargeoffid = ef.chargeoffid
    LEFT JOIN EXTENSION.REASONCODE rc ON rc.reasoncodeid = ef.reasoncodeid
    LEFT JOIN EXTENSION.APPROVALCODE ac ON ac.approvalcodeid = ef.approvalcodeid
    LEFT JOIN EXTENSION.FORMFOLLOWUP ffu ON ffu.formid = t.formid) t
        WHERE t.datesortkey = 1
     GROUP BY t.formid, t.statusname, t.formtype, t.chargeoffaccount, t.reasoncode, t.approvalcode
     ORDER BY t.formid
    

    The change I made to allow for FollowUpDate was to use a LEFT JOIN onto the FORMFOLLOWUP table – you were doing an INNER JOIN, so you’d only get rows with FORMFOLLOWUP records associated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks to a previous question I found a useful link on multiple buttons. http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx
I've been trying to figure out pointers in C most of today, even asked
I am a C/C++ programmer, but I was asked to update a program that
I understand that the title might not be descriptive enough, but I'm making a
I'm the second dev and a recent hire here at a PHP/MySQL shop. I
I need to develop a tool for web log data mining. Having many sequences
I've got a really strange error and any light that anyone can shed on
Update: As seen in the Original Questions below, I am looking to echo an
SOLVED see Edit 2 Hello, I've been writing a Perl program to handle automatic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.