sqldatabasejoinsubquerywhere-clause

Linking data based off a value within a group


Good afternoon, I have two tables (call them table a and b), the first has one row per id, with a number attributed too it as follows:

|  ID  | Number | 
|  1   |   10   |
|  2   |   21   |
|  3   |   09   |
|  4   |   19   |

The 2nd table is larger and has the same ID's, but multiple rows per ID. What I want is to be able to bring out every row of data that has a number either greater than or equal to the number in the table above:

e.g. ID 1 would bring back say number 10-15 if it had 15 rows of data ID 2 would bring back 21-29 etc

I can do it for an individual ID but how do I do it for multiple ID's?


Solution

  • If I understood correctly , maybe the following would help you.

    SELECT a.id,a.number,b.id,b.number
    FROM a 
    INNER JOIN b ON a.id=b.id AND b.number >= a.number;
    

    If you want to limit the search to specific id-s add a WHERE clause.