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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:28:07+00:00 2026-06-13T15:28:07+00:00

This is essentially a question about constructing an SQL query. The db is implemented

  • 0

This is essentially a question about constructing an SQL query. The db is implemented with sqlite3. I am a relatively new user of SQL.

I have two tables and want to join them in an unusual way. The following is an example to explain the problem.

Table 1 (t1):

id  year    name
-------------------------
297 2010    Charles
298 2011    David
300 2010    Peter
301 2011    Richard

Table 2 (t2)

id  year    food
---------------------------
296 2009    Bananas
296 2011    Bananas
297 2009    Melon
297 2010    Coffee
297 2012    Cheese
298 2007    Sugar
298 2008    Cereal
298 2012    Chocolate
299 2000    Peas
300 2007    Barley
300 2011    Beans
300 2012    Chickpeas
301 2010    Watermelon

I want to join the tables on id and year. The catch is that (1) id must match exactly, but if there is no exact match in Table 2 for the year in Table 1, then I want to choose the year that is the next (lower) available. A selection of the kind that I want to produce would give the following result

id  year    matchyr name    food
-------------------------------------------------
297 2010    2010    Charles Coffee
298 2011    2008    David   Cereal
300 2010    2007    Peter   Barley
301 2011    2010    Richard Watermelon

To summarise, id=297 had an exact match for year=2010 given in Table 1, so the corresponding line for id=297, year=2010 is chosen from Table 2. id=298, year=2011 did not have a matching year in Table 2, so the next available year (less than 2011) is chosen. As you can see, I would also like to know what that matched year (whether exactly , or inexactly) actually was.

I would very much appreciate (1) an indication (yes/no answer) of whether this is possible to do in SQL alone, or whether I need to look outside SQL, and (2) a solution, if that is not too onerous.

  • 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-13T15:28:08+00:00Added an answer on June 13, 2026 at 3:28 pm

    Sure, you can do this in SQL. What you need for the first step is a query which joins t1 and t2 on id, and gives you the various possible years (EDIT: fixed this sentence, the original one was incorporating the next step into it as well). You can create a view called t3 which gives you this as follows:

    CREATE VIEW [t3] AS
    SELECT t1.id as id, t2.year as year FROM t1 INNER JOIN t2 ON (t1.id = t2.id AND T2.year <= t1.year)

    From here, you’ll want to get the maximum year from t3 per id:

    CREATE VIEW [t4] AS
    SELECT id, MAX(year) FROM t3 GROUP BY id

    You should be able to join t4 back onto t1 and t2 to get what you want. Of course, there are ways to shorten this, but it should get you thinking along the right lines. Also, I’m guessing it’s just an example, but please don’t call your tables t1, t2, etc.

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

Sidebar

Related Questions

This is my fourth question here in about two weeks... I think I have
I have a use case with NServiceBus explained in this question. Essentially there is
I have a basic question about MPI programming in C. Essentially what I want
I am new to programming and I have a question about how to pull
I have a quick question about HTML and CSS. I thought this would be
This question is about the gui application (DB2 Control Center), not about sql statements
This question has been bugging me for a long time now but essentially I'm
This seems like a basic question, but I haven't found any clear answers. Essentially,
So I have a DOM document that looks essentially like this <categories> <category id=1/>
I have a schema that essentially looks like this: CREATE TABLE `data` ( `id`

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.