开发者

What's wrong with my SQL? (find a "previous" record)

My SQL-Query should return a previous record(Claim). Previous means that it has a different primary key (idData), an equal SSN_Number and an earlier Received_Date. The problem is that the Received_Date could be equal so I have to look for another column. The priority should be the same as the Sort-Order. What am I doing wrong, because the query returns for both ID's the other Record (both claims are the previous for each other)?

My two Test-Records are the following:

What's wrong with my SQL? (find a "previous" record)

(source: bilder-hochladen.net)

The SQL-Query is this:

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.idDat开发者_如何学JAVAa 
            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 

EDIT: according to Mongus Pong answer this is the correct 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


The problem is that you are returning the previous record if any one of those dates are less than the other.

Both records have got at least one date less than the other, so they return eachothers record.

According to what you specify you probably need something like :

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)

It looks horrendous, but thats SQL for you!


Looks like you get both records because

Claim.Repair_Completion_Date > PrevClaim.Repair_Completion_Date

is true for the second, and the other three clauses in your OR group are true for the first.

You may not wish to compare on Completion Date; perhaps just on one other field, like Claim_Submitted_Date?


The first row has a REPAIR_COMPLETION_DATE that is earlier than the second row's, which means that it will satisfy the JOIN condition Claim.Repair_Completion_Date > PrevClaim.Repair_Completion_Date, thus qualifying as a "previous" claim.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜