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:

(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.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
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 :
It looks horrendous, but thats SQL for you!