0

我试图只获得每个文件 ID 的一次出现,以及最后一次出现,我拉它的方式,即使我提供一个 FileID,它仍然返回多个。

SELECT  
            FM.FileNumber, FA.ReceivedDate AS LienSearchOrderDate, 
            FA1.ReceivedDate AS LienSearchReceivedDate, 
            DATEDIFF(dd, FA.ReceivedDate, FA1.ReceivedDate) AS TurnTimeDaysLienSearch, 
            FM.FileID, PC.Name, C.County, P.State, S.Name AS Status, 
            FM.ClientsFileNumber
FROM        dbo.FileMain AS FM 
INNER JOIN  dbo.FilePartnerRel AS FPR ON FM.FileID = FPR.FileID AND FPR.PartnerTypeID = 10011 
INNER JOIN  dbo.PartnerCompany AS PC  ON FPR.PartnerCompanyID = PC.PartnerCompanyID 
LEFT JOIN   dbo.FileActions AS FA     ON FM.FileID = FA.FileID 
LEFT JOIN   dbo.FileActions AS FA1    ON FA.FileID = FA1.FileID 
LEFT JOIN   dbo.ActionDef AS AD       ON FA1.ActionDefID = AD.ActionDefID 
INNER JOIN  dbo.Property AS P         ON FA1.FileID = P.FileID 
INNER JOIN  dbo.County AS C           ON P.CountyID = C.CountyID 
INNER JOIN  dbo.Status AS S           ON FM.StatusID = S.StatusID
WHERE FM.FileNumber = 'PA-22440' 
  and (FM.OpenedDate > '2012-10-01') 
  AND (FA.ActionDefID = 28) 
  AND (FA.ReceivedDate IS NOT NULL) 
  AND (FA.Live = 1) AND (FA1.ActionDefID = 183) 
  AND (FA1.Live = 1)
Group by FA.ReceivedDate, FM.FileNumber, FA1.ReceivedDate, FM.FileID, PC.Name, C.County, 
         P.State, S.Name, FM.ClientsFileNumber
Order by 1

我确定我过度使用查询,但我在这里尝试。我可以让它永远出现在底部,但我想让它拉每个实例 FileNumber。

这是我得到的输出:

FileNumber  LienSearchOrderDate LienSearchReceivedDate  TurnTimeDaysLienSearch  FileID  Name    County  State   Status  ClientsFileNumber
PA-22440    38:37.6 NULL    NULL    16448   NTIS    Pinellas    FL  Cancelled   test
PA-22440    40:08.8 NULL    NULL    16448   NTIS    Pinellas    FL  Cancelled   test

而预期的只是最后一项,仅此而已。

4

1 回答 1

0

如果这是您需要的,请尝试此查询

SELECT      FM.FileNumber, FA.ReceivedDate AS LienSearchOrderDate, 
            FA1.ReceivedDate AS LienSearchReceivedDate, 
            DATEDIFF(dd, FA.ReceivedDate, FA1.ReceivedDate) AS TurnTimeDaysLienSearch, 
            FM.FileID, PC.Name, C.County, P.State, S.Name AS Status, 
            FM.ClientsFileNumber
FROM        dbo.FileMain AS FM 
INNER JOIN  dbo.FilePartnerRel AS FPR ON FM.FileID = FPR.FileID AND FPR.PartnerTypeID = 10011 
INNER JOIN  dbo.PartnerCompany AS PC  ON FPR.PartnerCompanyID = PC.PartnerCompanyID 
LEFT JOIN   dbo.FileActions AS FA     ON FM.FileID = FA.FileID 
LEFT JOIN   dbo.FileActions AS FA1    ON FA.FileID = FA1.FileID 
LEFT JOIN   dbo.ActionDef AS AD       ON FA1.ActionDefID = AD.ActionDefID 
INNER JOIN  dbo.Property AS P         ON FA1.FileID = P.FileID 
INNER JOIN  dbo.County AS C           ON P.CountyID = C.CountyID 
INNER JOIN  dbo.Status AS S           ON FM.StatusID = S.StatusID
WHERE FM.FileNumber = 'PA-22440' 
  and (FM.OpenedDate > '2012-10-01') 
  AND (FA.ActionDefID = 28) 
  AND (FA.ReceivedDate IS NOT NULL) 
  AND (FA.Live = 1) AND (FA1.ActionDefID = 183) 
  AND (FA1.Live = 1)
  AND (FA.ReceivedDate = (SELECT MAX(ReceivedDate) FROM FileActions WHERE ActionDefID = 28 AND ReceivedDate IS NOT NULL AND Live = 1 AND FileID = FM.FileID))
Order by FM.FileNumber

如果您的 FileActions 表中有一个主键(我希望您有),请使用它来匹配正确的记录,而不是与 ReleaseDate 匹配。像这样的东西:

AND FA.PrimaryKey = (SELECT TOP 1 PrimaryKey FROM FileActions WHERE FileID = FM.FileID ORDER BY ReleaseDate DESC)
于 2013-09-24T21:57:16.300 回答