I'm taking my first steps in terms of practical SQL use in real life.
I have a few tables with contractual and financial information and the query works exactly as I need - to a certain point. It looks more or less like that:
SELECT /some columns/ from CONTRACTS
Linked 3 extra tables
with INNER JOIN to add things like department names, product information etc. This all works but they all have simplish one-to-one relationship (one contract related to single department in Department
table, one product information entry in the corresponding table etc).
Now this is my challenge:
I also need to add contract invoicing information doing something like:
inner join INVOICES on CONTRACTS.contnoC = INVOICES.contnoI
(and selecting also the Invoice number
linked to the Contract number
, although that's partly optional)
The problem I'm facing is that unlike with other tables where there's always one-to-one relationship when joining tables, INVOICES
table can have multiple (or none at all) entries that correspond to a single contract no. The result is that I will get multiple query results for a single contract no (with different invoice numbers presented), needlessly crowding the query results.
Essentially I'm looking to add INVOICES
table to a query to just identify if the contract no is present in the INVOICES
table (contract has been invoiced or not). Invoice number itself could be presented (it is with INNER JOIN
), however it's not critical as long it's somehow marked. Invoice number
fields remains blank in the result with the INNER JOIN
function, which is also necessary (i.e. to have the row presented even if the match is not found in INVOICES
table).
SELECT DISTINCT
would look to do what I need, but I seemed to face the problem that I need to levy DISTINCT
criteria only for column representing contract numbers, NOT any other column (there can be same values presented, but all those should be presented).
Unfortunately I'm not totally aware of what database system I am using.