左外连接视图中的最大日期

时间:2009-08-18 16:24:12

标签: sql tsql

感谢上一个问题,我发现了如何根据链接表提取最新数据。但是,现在我有一个相关的问题。

我找到的解决方案使用row_number()和PARTITION来提取最新的数据集。但是如果视图中链接表中有零行或多行的可能性怎么办?例如,表FollowUpDate可能有0行,或1或更多。我只想要最新的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 

如果我使用row_number()和PARTITION进行类似的拉取,只有在FollowUpDate中至少有一行时才会获取数据。有点打败LEFT OUTER JOIN的目的。任何人都可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

我重写了你的查询 - 你有不必要的子选择,并使用row_number()作为FUDat​​eSortKey但没有使用该列:

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

我允许FollowUpDate的更改是在FORMFOLLOWUP表上使用LEFT JOIN - 您正在进行INNER JOIN,因此您只能获得与FORMFOLLOWUP记录关联的行。

答案 1 :(得分:0)

如果没有表格定义和样本数据,很难猜到发生了什么。

此外,这令人困惑:“表FollowUpDate可能有0行”并且您“想要最新的FollowUpDate”。 (特别是当没有名为FollowUpDate的表时)如果FollowUpDates为零,则没有“最近的FollowUpDate”。

也许你想要

WHERE <follow up date row number> in (1,NULL)

答案 2 :(得分:0)

我明白了。像往常一样,我需要小睡一会儿。我只需要将我的子选择改为我会发誓我试过没有成功的东西:

SELECT field1, field2     
FROM Table1 t1    
LEFT JOIN (    
      SELECT field3, max(dateColumn)    
      FROM Table2    
      GROUP BY    
            field3    
) t2    
ON (t1.field1 = t2.field3)