-4

I have two tables (A and B), the table A contains the data of reports: enter image description here

and table B, showing the exclusion data of these reports: enter image description here

I would like to do a JOIN having as a resul the 'Exclude_reason' only for the report with inventoryreport_id = 7 only. The column inventoryreport_id (table B) comes from column id (table A)

I tried different types of join but nothing gives me the result I am looking for. As result of RIGHT Join, I got this data, but this is not what I am looking for: enter image description here

What I would like to have is the last 4 rows of table B.

  • 6
    [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Thom A Aug 31 '23 at 13:37
  • 2
    What is it you need help with? What have you tried? Why isn't it working? What about the content you're read about `JOIN`s (which you appear to be asking about) don't you understand? – Thom A Aug 31 '23 at 13:37
  • I have tried these types of join: inner, left and right join select ar.[id], ar.[report_name], exc.[exclude_reference], exc.[exclude_reason] from awsinventoryreport_exceptions exc right join awsInventoryReport ar on ar.[id] = exc.[id] where ar.report_name like '%report7%' Gives me only one result, which is wrong, I want the 4 last rows from table B as result id report_name exclude_reference exclude_reason 7 report7 NULL NULL – Trying to be DBA Aug 31 '23 at 13:40
  • 1
    Can you share your desired results from this sample data so we can understand better. Also, what column(s) are you joining on? It's not immediately obvious in your sample data how these two tables relate to one another. – JNevill Aug 31 '23 at 13:51
  • select ar.[id], ar.[report_name], exc.[exclude_reference], exc.[exclude_reason] from awsinventoryreport_exceptions exc right join awsInventoryReport ar on ar.[id] = exc.[id] where ar.report_name like 'report7' - this is joing I did, I would expect as result the last 4 columns of table B – Trying to be DBA Aug 31 '23 at 14:04
  • This may help you understand the basics of the different joins. https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ – Sean Lange Aug 31 '23 at 14:24
  • Please clarify via edits, not comments. [mre] [ask] [Help] – philipxy Aug 31 '23 at 18:02
  • @SeanLange That blog is useless & misleading & repudiated by its author in its comments, although their comment shows further misconceptions. Also: [Such Venn-like diagrams for joins are unclear, unhelpful & misleading.](https://stackoverflow.com/a/55642928/3404097) See [my Q&A comments here.](https://stackoverflow.com/q/38549/3404097) "All you have to do to see this is try to write a correct legend for one." Don't forget that SQL tables are bags not sets. Joins are on any condition & don't need constraints so at best the diagrams when explained address special cases and/or partial properties. – philipxy Aug 31 '23 at 18:06

0 Answers0