question

RamKumar88 avatar image
RamKumar88 asked

SQL - Inner Join

I've a table like below. I need to compare the tables (Table A & Table B) and I need to display the output only if table B value is plus or minus 150 when it is compared with the respective record of Table A. I tried this task with inner join and similar stuff but it never helped. Please suggest. Table A: cost 1000 1020 1100 345 5678 12670 Table B: cost 1150 970 2345 700 5500 11000 I need the output like below. cost 1150 970 5500
joins
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Dave_Green avatar image Dave_Green ♦ commented ·
Can you show us the code you have tried? It will help us see the approaches you have taken, and we can then guide you to a solution rather than just giving you the answer. Also do you mean the values should be within 150 or exactly 150 apart?
2 Likes 2 ·
Squirrel avatar image Squirrel commented ·
how did the cost 5500 comes from ? You are comparing it with which record ?
1 Like 1 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
But there's nothing that's showing us what connects Table A to Table B. 'INNER JOIN ON tablea.colx = tableb.colx' or something along those lines is what's needed. You're showing three output values that do NOT have a match between the two tables, so INNER JOIN is absolutely not going to work. You need to explain better what it is that you're trying to do.
0 Likes 0 ·

1 Answer

·
GPO avatar image
GPO answered
I'll make a few assumptions here. Try this and let us know whether it answers your question: SELECT b.cost FROM table_b as b JOIN table_a as a ON b.cost >= a.cost - 150 and b.cost
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.