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

  • SEARCH
  • Home
  • 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 186207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:36:23+00:00 2026-05-11T15:36:23+00:00

I have a database where I need to query to get records from one

  • 0

I have a database where I need to query to get records from one table and then lookup on another table to see if that value exists there. That table might return multiple records and I want the one with the most recent date.

So table 1 is basically:

ID (Primary Key) Name Test_Score 

And Table 2 is

Test_Id (Primary) Student_ID (Foreign Key) Test_Score Test_Date 

As I’m going through the records, if the no tests exist in table 2 for the Student_id, I want to use the score in table 1, otherwise I want to use the score from table 2 with the most recent date. I have this all working in C# code, but the clients want it in a stored procedure for reporting purposes and I’m seeing some performance issues since the tables are quite large. Also, this basic example actually happens multiple times against multiple tables.

I’m sure there is an elegant way of doing this that is fast and efficient, but I can’t seem to come up with anything but using a cursor.

Does someone know the straight forward solution?

  • 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. 2026-05-11T15:36:23+00:00Added an answer on May 11, 2026 at 3:36 pm

    Not 100% percent sure about syntactical details, but something like this:

    select table1.Name, ISNULL(table2.Test_Score, table1.Test_Score) from    table1   left outer join table2     on table1.id = table2.Student_ID     AND table2.Test_Date = (       select max(x.Test_Date)       from table2 x       where table1.id = x.Student_ID       group by x.Student_ID) 

    If the subquery is not allowed where it is, move it to the where clause. (Sorry, i can’t try it where i am now.)

    The query only works if the Test_Date is unique. If not, you get repeated results. Then you should use a Group By

    select table1.Name, min(ISNULL(table2.Test_Score, table1.Test_Score)) from    table1   left outer join table2     on table1.id = table2.Student_ID     AND table2.Test_Date = (       select max(x.Test_Date)       from table2 x       where table1.id = x.Student_ID       group by x.Student_ID) group by table1.id, table1.Name 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database with a table that records data coming from various sources.
I have a database table that holds information for received files. One of the
Ok I creating an application that needs to query records from my remote database.
I have a form I need to insert some information into one table. Then
I have an access database, with a query made. I need to automate it
I have a name of table or view in PostgreSQL database and need to
I have a script that appends some rows to a table. One of the
How does one filter a list of records to remove those that have some
Here's the situation: we have an Oracle database we need to connect to to
I have a database full of small HTML documents and I need to programmatically

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.