2010-07-16 39 views
4

我的SQL查詢應返回以前的記錄(索賠)。 Previous表示它具有不同的主鍵(idData),相等的SSN_Number和更早的Received_Date。問題是Received_Date可能相等,所以我必須尋找另一列。優先級應與排序順序相同。 我在做什麼錯誤,因爲查詢返回兩個ID的另一個記錄(兩個索賠都是以前的對方)?我的SQL有什麼問題? (查找「前一個」記錄)

我的兩個測試,記錄如下: same SSN_Number http://www.bilder-hochladen.net/files/4709-kr.jpg

的SQL查詢是這樣的:

SELECT TOP (1) Claim.idData  AS ClaimID, 
       PrevClaim.idData AS PrevClaimID 
FROM tabData AS Claim 
     INNER JOIN tabData AS PrevClaim 
     ON Claim.SSN_Number = PrevClaim.SSN_Number 
      AND Claim.idData <> PrevClaim.idData 
      AND (Claim.Received_Date > PrevClaim.Received_Date 
        OR Claim.Claim_Creation_Date > PrevClaim.Claim_Creation_Date 
        OR Claim.Repair_Completion_Date > 
         PrevClaim.Repair_Completion_Date 
        OR Claim.Claim_Submitted_Date > 
         PrevClaim.Claim_Submitted_Date) 
WHERE (Claim.idData = @claimID) 
ORDER BY PrevClaim.Received_Date DESC, 
      PrevClaim.Claim_Creation_Date DESC, 
      PrevClaim.Repair_Completion_Date DESC, 
      PrevClaim.Claim_Submitted_Date DESC 

編輯:根據Mongus傍回答這是正確的SQL:

SELECT TOP (1) Claim.idData AS ClaimID, PrevClaim.idData AS PrevClaimID 
FROM tabData AS Claim INNER JOIN tabData AS PrevClaim 
     ON Claim.SSN_Number = PrevClaim.SSN_Number 
     AND Claim.idData <> PrevClaim.idData 
     AND (Claim.Received_Date > PrevClaim.Received_Date 
     OR Claim.Received_Date = PrevClaim.Received_Date AND Claim.Claim_Creation_Date > PrevClaim.Claim_Creation_Date 
     OR Claim.Received_Date = PrevClaim.Received_Date AND Claim.Claim_Creation_Date = PrevClaim.Claim_Creation_Date AND Claim.Repair_Completion_Date > PrevClaim.Repair_Completion_Date 
     OR Claim.Received_Date = PrevClaim.Received_Date AND Claim.Claim_Creation_Date = PrevClaim.Claim_Creation_Date AND Claim.Repair_Completion_Date = PrevClaim.Repair_Completion_Date AND Claim.Claim_Submitted_Date > PrevClaim.Claim_Submitted_Date) 
WHERE (Claim.idData = @claimID) 
ORDER BY PrevClaim.Received_Date   DESC, 
     PrevClaim.Claim_Creation_Date DESC, 
     PrevClaim.Repair_Completion_Date DESC, 
     PrevClaim.Claim_Submitted_Date DESC 

回答

3

問題是如果這些日期中的任何一個比其他日期少,您將返回以前的記錄。

兩個記錄至少有一個日期比另一個少,所以他們返回eachothers記錄。

根據您指定的內容,你可能需要的東西,如:

Claim.Received_Date > PrevClaim.Received_Date OR 
(Claim.Received_Date = PrevClaim.Received_Date AND Claim.Claim_Creation_Date > PrevClaim.Claim_Creation_Date) OR 
(Claim.Received_Date = PrevClaim.Received_Date AND Claim.Claim_Creation_Date = PrevClaim.Claim_Creation_Date AND Claim.Repair_Completion_Date > PrevClaim.Repair_Completion_Date) OR 
(Claim.Received_Date = PrevClaim.Received_Date AND Claim.Claim_Creation_Date = PrevClaim.Claim_Creation_Date AND Claim.Repair_Completion_Date = PrevClaim.Repair_Completion_Date AND Claim.Claim_Submitted_Date > PrevClaim.Claim_Submitted_Date) 

它看起來可怕,但多數民衆贊成SQL爲您服務!

+0

謝謝。這是行得通的。我認爲它一定是這樣的,但我對此感到困惑:) – 2010-07-16 15:34:15

1

看起來你得到兩個記錄,因爲

Claim.Repair_Completion_Date > PrevClaim.Repair_Completion_Date 

對於第二個是正確的,您的OR組中的其他三個子句對於第一個是正確的。

您可能不希望在完成日期進行比較;也許只是在另一個領域,像Claim_Submitted_Date?

+0

打不到一分鐘:P – 2010-07-16 15:22:36

1

第一行有一個REPAIR_COMPLETION_DATE,比第二行早,這意味着它將滿足JOIN條件Claim.Repair_Completion_Date > PrevClaim.Repair_Completion_Date,因此符合「先前」聲明的條件。