我試圖得到每個文件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
和預期的僅僅是最後一個項目,僅此而已。
你應該**在這個查詢中縮小了問題**。首先從'SELECT'中刪除大部分內容,然後逐個刪除'JOIN',以便查看會發生什麼情況。正如所寫,我們必須深入研究查詢以尋找可能的問題,並且如果沒有給出示例輸入以及預期的和實際的輸出,肯定會涉及相當多的猜測工作。爲了澄清,還**給我們示例輸入和預期和實際輸出**。 – Dukeling
爲什麼在這裏按'1'排序? – RBarryYoung
@RBarryYoung:我只是在嘗試新的東西。 –