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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:54:52+00:00 2026-06-07T06:54:52+00:00

So I have 2 tables I need to join. Table1 includes a column called

  • 0

So I have 2 tables I need to join. Table1 includes a column called JDAY which hold the values 1.5, 2.5, 3.5…365.5. It looks like:

JDAY
1.5
2.5
3.5
4.5
5.5
etc.

I would like to join it with Table2, which looks like:

JDAY  WSC
1     1
5     .9
8     .7
366   .5

The final result should have a JDAY column with all the values from Table1, and the WSC value from Table2 corresponding to the closest JDAY value less than or equal to that in Table2. For example, JDAY=5.5 in Table1 corresponds to WSC=.9, because 5.5 is between 5 and 8. It would look like this:

JDAY   WSC
1.5    1
2.5    1
3.5    1
4.5    1
5.5    .9
6.5    .9
7.5    .9
8.5    .7
9.5    .7
etc.
  • 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-07T06:54:54+00:00Added an answer on June 7, 2026 at 6:54 am

    One way to get the result is to use a correlated subquery:

    UPDATED

    SELECT t1.JDAY
         , (SELECT t2.WSC FROM Table2 t2 
             WHERE t2.JDAY < t1.JDAY
             ORDER BY t2.JDAY DESC LIMIT 1
           ) AS WSC
      FROM Table1 t1
     ORDER BY t1.JDAY 
    

    You noted you wanted the largest value of WSC less than JDAY; I’m taking that to mean that you want the maximum value of WSC from the rows in Table2 that have a JDAY value LESS THAN the JDAY value from the Table1 row. That seems to fit with the result set you are showing. (If I got that wrong (which I may have), it was my misunderstanding, reading something you didn’t intend, if so, please feel free to correct me.)

    UPDATE

    I have a better understanding, you want the value of WSC from the row in Table2 that has the largest JDAY value that is earlier than the JDAY value in Table1. You want the WSC from the row with with the latest earlier date. The queries in my answer are modified to satisfy this requirement.


    Another alternative to get basically the same result set is to use a theta-join (that is, a join on an inequality predicate)

    UPDATED

    SELECT t.JDAY
         , s.WSC
      FROM ( SELECT t1.JDAY
                  , MAX(t2.JDAY) AS max_t2_jday
               FROM Table1 t1
               LEFT
               JOIN Table2 t2
                 ON t2.JDAY < t1.JDAY
              GROUP BY t1.JDAY
              ORDER BY t1.JDAY
           ) t
      LEFT
      JOIN Table2 s
        ON s.JDAY = t.max_t2_jday
    

    That will work as long as there aren’t any “duplicate” values of JDAY in Table2. If there are duplicate values, you’ll need to have a way to eliminate any duplicates that get returned. Adding a GROUP BY t.JDAY would be sufficient, but not deterministic. (That is, you wouldn’t be guaranteed which row you were getting the WSC value from.)

    Here, we are asking for every row from Table1, AND any “matching” rows from Table2, matching on the JDAY from Table2 being LESS THAN the JDAY from Table1. For each row in Table1, we will rummage through all the “matching” rows from Table2 (if any) and we will pick out the largest WSC value matching JDAY from Table2 and return it in the subquery (inline view) aliased as t. We can then join that result set back to Table2, to get the matching row, along with the WSC value on that row.

    One difference between these queries comes into play when there are “duplicate” values of JDAY in Table1, they will return slightly different result sets. The first query will return all the rows from Table1, including duplicate values of JDAY. The second query will remove duplicate values of JDAY from Table 1, and return a distinct set of values for JDAY.


    UPDATE:

    I’ve updated the answer above, not getting the MAX(WSC) value, but instead getting the WSC value from the row in Table2 with the closest JDAY that is less than the Table1 JDAY.

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

Sidebar

Related Questions

I have 3 tables I need to join. The contracts table is the main
I have 3 tables which need to be linked in an SQL statement (I'm
I have a table with more than 100 columns. I need to join it
I have multiple tables that need to be merged into one. SELECT name, SUM(money)
I have a couple of tables that need to be be joined. The tables
I have 2 tables in a DB with very similar schemas that need to
I have 3 tables, I need advice on how to get data from them.
I have two tables described below. What I need is a single query that
i have 5 tables,in every table it has 4 rows. so i need to
I have two tables : teachers (teacher_id,teacher_name) courses (teacher_id,course_id) And I need to display

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.