2013-09-24 50 views
0

我試圖得到每個文件ID只有一個發生,最後一個,我拉它的方式,即使我提供一個FileID,它仍然返回更多不止一個。拉最後一次出現的文件編號在SQL

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 

我確定我有一些過度使用查詢,但我在這裏嘗試。我可以讓它得到EVER的底部出現,但我想讓它拉取每個實例的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 

和預期的僅僅是最後一個項目,僅此而已。

+0

你應該**在這個查詢中縮小了問題**。首先從'SELECT'中刪除大部分內容,然後逐個刪除'JOIN',以便查看會發生什麼情況。正如所寫,我們必須深入研究查詢以尋找可能的問題,並且如果沒有給出示例輸入以及預期的和實際的輸出,肯定會涉及相當多的猜測工作。爲了澄清,還**給我們示例輸入和預期和實際輸出**。 – Dukeling

+0

爲什麼在這裏按'1'排序? – RBarryYoung

+0

@RBarryYoung:我只是在嘗試新的東西。 –

回答

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) 
+0

我已經改變了連接,它仍然提供2個結果。 –

+0

似乎問題出在這裏 - dbo.FileActions AS FA - 即在FileMain表中的一行中,該表中有兩行。您會注意到第2列中的不同值,它指出了問題的根源。 – lot

+0

我調整了它,它仍然沒有按照預期拉動。 –

相關問題