0

FileID を指定しても、各ファイル ID の最後の 1 つだけを取得しようとしています。

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 に答える