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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:41:56+00:00 2026-06-03T07:41:56+00:00

This is a very basic SQL question. What I’ve got is a database for

  • 0

This is a very basic SQL question. What I’ve got is a database for a garage with two tables: CARS and SERVICES. Each car is brought in for servicing once in a while, so each car can have multiple SERVICES records. Each record in SERVICES has a foreign key pointing to the related car, a date field and some additional info fields.

What I want is to get a list of cars with all the info from the most recent servicing. If I simply do

SELECT C.ID, S.*
FROM CARS C
JOIN SERVICES S ON S.CAR_ID = C.ID

Then I get a list of all cars with all services for each car (the Cartesian product of the two data sets). I’m using MS SQL Server.

  • 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-06-03T07:41:57+00:00Added an answer on June 3, 2026 at 7:41 am

    If you are using sql 2005+. Then you can do this:

    ;WITH CTE
    AS
    (
    SELECT
        ROW_NUMBER() OVER(PARTITION BY CAR_ID 
                           ORDER BY theDateTimeColumn DESC) AS RowNbr,
        s.*
    FROM
        SERVICES AS s
    )
    SELECT
        *
    FROM
        CARS AS c
        JOIN CTE
            ON c.ID=CTE.CAR_ID 
            AND CTE.RowNbr=1
    

    EDIT

    If you want to have all the Cars that no matter if there has be serviced then you need to use a LEFT JOIN instead of a JOIN. Like this:

    ;WITH CTE
    AS
    (
    SELECT
        ROW_NUMBER() OVER(PARTITION BY CAR_ID 
                           ORDER BY theDateTimeColumn DESC) AS RowNbr,
        s.*
    FROM
        SERVICES AS s
    )
    SELECT
        *
    FROM
        CARS AS c
        LEFT JOIN CTE
            ON c.ID=CTE.CAR_ID 
            AND CTE.RowNbr=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a basic question but I'm not very proficient in SQL Server.
This may be very basic question. If we open a sql transaction with Begin
This is very basic magento question i guess. I want to first get all
This is very basic question from programming point of view but as I am
This is a very basic question, so please bear with me. Consider the following
This is a very basic question...quite embarassing, but here goes: I have a Stopwatch
This is a very very basic question and I know one way is to
This is a very basic question, but my brain isn't working and multiple attempts
This could be very basic question. I'd like to validate a variable is type
This is a very basic MySQL question but I could not find an answer.

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.