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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:53:33+00:00 2026-05-13T20:53:33+00:00

My application has a table that contains snapshot inventory data from each year. For

  • 0

My application has a table that contains snapshot inventory data from each year. For example, there’s a vehicle inventory table with the typical columns vehicle_id, vehicle_plate_num, vehicle_year, vehicle_make, etc, but also the year designating that the vehicle is owned.

Querying the entire table might result in something like this:

Id  Plate Num   Year Make     Model    Color   Year Owned
---------------------------------------------------------
1   AAA555      2008 Toyota   Camry    blue    2009
2   BBB666      2007 Honda    Accord   black   2009
3   CCC777      1995 Nissan   Altima   white   2009
4   AAA555      2008 Toyota   Camry    blue    2010
5   BBB666      2007 Honda    Accord   black   2010
6   DDD888      2010 Ford     Explorer white   2010

(Good or bad, this table already exists and it is not an option to redesign the table and that’s a topic for another question). What you see here is year after year, the majority of the vehicles are still in the inventory, but there’s always the situation where old ones are getting rid of, and new vehicles are acquired. In the example above, the 1995 Nissan Altima was in the 2009 inventory but no longer in the 2010 inventory. The 2010 inventory has a new 2010 Ford Explorer.

How can I build an efficient query that takes any two years and show only the difference. For example, if I pass in 2009, 2010, the query should returns

3   CCC777  1995  Nissan  Altima      white   2009

If I pass in 2010, 2009, the query should return

6   DDD888   2010  Ford   Explorer   white   2010

Edit:
I should have added the comment following the answer from Kyle B., but the text area for comment is not very user-friendly:

I didn’t think it would be this tough, but seems to be.

Anyway, wouldn’t you need a sub-select from the above like this:

select q.* from (
    select f.*
    from inventory f 
      left join inventory s
      on (f.plate_num = s.plate_num 
         and f.year_owned = :first-year
         and s.year_owned = :second-year)
    where s.plate_num is null
) q
where q.year_owned = :second_year
  • 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-13T20:53:33+00:00Added an answer on May 13, 2026 at 8:53 pm

    You want a self-outer join

    It looks like you want the asymmetric difference. If you wanted the symmetric difference, you’d use a full outer join instead of a left (or right) outer join.

    With variables :first-year and :second-year

    select f.*
       from inventory f 
         left join inventory s
            on (f.plate_num = s.plate_num
               and s.year_owned = :second-year)
    where s.plate_num is null 
      and f.year_owned = :first-year
    

    Note that the condition has to be inside the join condition, so that database will return a null row when there’s no match instead of finding a match that later gets removed by filtering.

    Edit: Adjusted query slightly. This doesn’t require a sub-select. Tested with postgresql.

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

Sidebar

Related Questions

My application database has a Groups table that separates users into logical roles and
My application has a need to let the user choose a date from a
My application has a plug-in model that allows third-party developers to write assemblies that
I believe the application has some parts that target .NET, and some that don't.
My web application has a login page that submits authentication credentials via an AJAX
I have a MyISAM table that basically contains a log. A cluster of machines
I have to maintain an application that has a lot of columns that are
In my application, a user has_many tickets. Unfortunately, the tickets table does not have
Application has an auxiliary thread. This thread is not meant to run all the
Our application has a file format similar to the OpenDocument file format (see http://en.wikipedia.org/wiki/OpenDocument

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.