Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 921447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:52:34+00:00 2026-05-15T18:52:34+00:00

My SQL-Query should return a previous record(Claim). Previous means that it has a different

  • 0

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:
same SSN_Number
(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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:52:35+00:00Added an answer on May 15, 2026 at 6:52 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that generates an SQL query as text e.g. ... return
I have an SQL query that takes the following form: UPDATE foo SET flag=true
This Linq to SQL query ... Return (From t In Db.Concessions Where t.Country =
I need a SQL query that returns ContactDate, SortName, City, ContactType, and Summary from
I have a sql query in MySQL and I want an expression that matches
This SQL query was generated by Microsoft Access 2003, and works fine when run,
What SQL query shows me the tables & indexes used by a view on
What is the SQL query to select all of the MSSQL Server's logins? Thank
What is the simplest SQL query to find the second largest integer value in
When you execute a SQL query, you have to clean your strings or users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.