I have searched Google and Stack Overflow for a answer to my query, but I feel my lack of SQL vocabulary is standing in my way of finding an answer as I believe this would be a common question. Any helpful points in the direction of what I need to be reading are always welcome.
On to the question, I’m attempting to join three tables in Oracle 8i, for example, a Company Table, an Invoice Table and a Jobs Table there is no link between the Invoice Table and the Jobs Table. I am hoping that in one query I can link all three tables returning all invoices and all jobs for a company without returning all jobs for each invoice (see my example results below).
I want to see:
Company 1 Invoice 1 Job 1
Company 1 Invoice 2 Job 2
Company 1 Invoice 3 Job 3
I don’t want to see:
Company 1 Invoice 1 Job 1
Company 1 Invoice 1 Job 2
Company 1 Invoice 1 Job 3
Company 1 Invoice 2 Job 1
Company 1 Invoice 2 Job 2
Company 1 Invoice 2 Job 3
Company 1 Invoice 3 Job 1
Company 1 Invoice 3 Job 2
Company 1 Invoice 3 Job 3
As always thank you for any help you can offer.
EDIT:
Essentially both the Invoice and Job Tables both have a Company Table Key field it’s just that the Job and Invoice table have no link to each other directly. If the instance comes up when there are 2 Invoices and 3 Jobs I’d ideally like it to show and vice versa:
Company 1 Invoice 1 Job 1
Company 1 Invoice 2 Job 2
Company 1 Job 3
Although looking at the problem like this makes me feel that this is further away from an easier answer than I hoped.
Your requirement means that you have a problem with your schema. My first advice in this case would be to modify your schema: add a
job_idtoinvoiceor aninvoice_idtojob(or a N-N relationship tableinvoice_job).If you’re not willing to update your schema, you can work a query that will make the join. The following query will basically join
jobandinvoiceone-to-one:The join condition here is artificial. If you remove an invoice, the jobs and invoices relationship may change.
If the two tables are really unrelated, you may want instead to display the results differently, for example: