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 7700401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:41:59+00:00 2026-05-31T22:41:59+00:00

I have an SQL query (below) that essentially takes a student from tbStudents, and

  • 0

I have an SQL query (below) that essentially takes a student from tbStudents, and then gets the most recent term (a number, but stored as a string) in tbTerms.

There is a one-to-many relationship with a student in tbStudents to a term record in tbTerms. Example:

tbStudents:

StudentId FirstName LastName
12345     John      Smith
12346     Jane      Doe

tbTerms:

StudentId Term
12345     1234
12345     1235
12345     1236
12346     1233
12346     1234

Desired:

StudentId FirstName LastName Term
12345     John      Smith    1236
12346     Jane      Doe      1234

SQL Query:

select tbStudents.student_id, tbStudents.user_id, tbStudents.firstname, tbStudents.lastname, v.rTerm
    from  tbStudents
    inner join (
        select tbTerms.student_id, MAX(tbTerms.term) as rTerm
        from tbTerms
        group by tbTerms.student_id
    ) v on v.student_id = tbStudents.student_id

I’ve been trying to get this all into one LINQ statement, but I’m having trouble. Is there anyway to do this in one statement? Or must it be done in multiple statements. Thanks in advanced.

Edit: C# code of what I have tried.

var students = (from s in dockDb.tbStudents
                join t in dockDb.tbTerms on s.student_id equals t.student_id
                into pairs
                from p in pairs
                select new { UserId = s.user_id, StudentId = s.student_id, Term = p.term } ).ToList();

Output is similar to:

StudentId FirstName LastName Term
12345     John      Smith    1234
12345     John      Smith    1235
12345     John      Smith    1236
12346     Jane      Doe      1233
12346     Jane      Doe      1234

Edit #2: I’m using Entity Framework for data. I’m not sure if this affects anything, but most of the solutions are ‘Syntactically incorrect’ when I attempt them.

  • 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-31T22:42:00+00:00Added an answer on May 31, 2026 at 10:42 pm

    Should be possible. Try this:

    from s in Students
    join t in Terms on t.StudentId = s.StudentId
    select new {Student = s, Term = t}
    group by s into g
    select new {g.Key.StudentId, g.Key.FirstName, g.Key.LastName, g.Max(x=>x.Term.term)};
    

    Breaking it down, the first two lines define the domain in which to search; namely, the inner-joined combination of Students and Terms, linked by StudentId. Each unique combination of Student and Term are put into an anonymous type, and then those instances are grouped by each unique Student into a Lookup (a read-only collection of IEnumerables referenced by key). The, we can select the key’s (Student’s) vital info, and in the list of terms, find the one with the highest value (which will be the most recent).

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

Sidebar

Related Questions

I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a SQL query that takes a very long time to run on
I have a Pl/SQL query that pull all records from the 3 tables. That
I have sql query like shown below the sql server says that their is
I have RO access on a SQL View. This query below times out. How
I have a problem with the SQL statement detailed below. The query returns the
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have a sql query that runs super fast, around one second, when not
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have this sql search query that works, but it only works if votes

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.