Duplicate results on inner join - sql

I've written the below query but I'm getting multiple duplicate rows in the results, please can anyone see where I'm going wrong?
use Customers
select customer_details.Customer_ID,
customer_details.customer_name,
metering_point_details.MPAN_ID,
Agents.DA_DC_Charge
from Customer_Details
left join Metering_Point_Details
on customer_details.customer_id = Metering_Point_Details.Customer_ID
left join agents
on customer_details.Customer_ID = agents.customer_id
order by customer_id

It doesn't really matter, but you're not using an INNER JOIN. Regardless, your unexpected rows indicate that your JOIN criteria is not specific enough to return your expected output. You can use SELECT DISTINCT if your results are fully duplicative, and if you'd like to see why you're getting those duplicates you can just use SELECT * to see the full detail between the multiple rows that are returned using your JOIN criteria, which should help you either make your criteria more specific or show you that you've got duplicated records in one of the tables you're using in your JOIN.
With sample data we can dissect the problem more, but odds are you won't need it once you see why the rows are duplicated.

Related

ORA-01417 - Two outer joins error. New join syntax?

I never got my head around the "new" SQL join syntax, and therefore use the "old" join system, with the (+). I know it's about time I learned it - however I just find the old syntax a lot more intuitive, especially when working with multiple tables with multiple joins.
However I now have an operation which requires two outer joins on the same table. My code is:
SELECT
C.ID,
R.VALUE,
R.LOG_ID,
LOG.ACTION
FROM
C,
R,
LOG
WHERE
C.DELETED IS NULL
AND R.DELETED IS NULL
-- Two joins below
AND R.C_ID(+) = C.ID
AND R.LOG_ID(+) = LOG.ID
However this results in an error:
ORA-01417 - A table may be outer joined to at most one table.
Searching for this error I find that the solution is to use the new syntax For example this answer on SO:
Outer join between three tables causing Oracle ORA-01417 error
So I am aware that some may consider this question a duplicate as it technically already has an answer. However the "old" syntax posed in that question does not contain exactly the same number of tables and joins as I have here, and try as I might, I'm not sure how I would factor this in to my own code.
Is anyone able to assist? Thanks.
I think you want:
SELECT C.ID, R.VALUE, R.LOG_ID, LOG.ACTION
FROM C LEFT JOIN
R
ON R.C_ID = C.ID LEFT JOIN
LOG
ON R.LOG_ID = LOG.I
WHERE C.DELETED IS NULL AND
R.DELETED IS NULL;
The "new" (it is 25 years old) outer join syntax is actually very easy to follow, particularly for a simple example with just LEFT JOIN.
The idea is you want to keep all rows from one table (perhaps subject to filters in the WHERE clause). That is the first table. Then you use a chain of LEFT JOIN to bring in other tables.
All rows from the first table are in the result set. If there are matching rows in the other tables, then columns from those tables come from matching rows. If there are no matches, then the row from the first table is kept.

How do I remove duplicate rows on returned related data in SQL?

I have a query that joins many related tables together and as a result, it returns duplicate rows for those items with multiple data against them.
I've searched for answers to this on stack and via google but all the results show things like using 'DISTINCT' or creating a subquery. I can't get any solution to work and I think the confusion I face is because of the many joins I have.
Can someone guide me on how to stop my results shows duplicates? Here is my query so far.
SELECT dbo.Vessel.VesselId,
dbo.Vessel.Name,
dbo.Capacity.DeckAreaM2,
dbo.Vessel.DPClassId,
dbo.Subsea.Accomodation,
dbo.Subsea.RovHangar,
dbo.Crane.SWL
FROM dbo.Vessel INNER JOIN
dbo.Capacity ON dbo.Vessel.VesselId = dbo.Capacity.VesselId LEFT OUTER JOIN
dbo.DeckEquipment ON dbo.Vessel.VesselId = dbo.DeckEquipment.VesselId LEFT OUTER JOIN
dbo.Crane ON dbo.DeckEquipment.DeckEquipmentId = dbo.Crane.DeckEquipmentId LEFT OUTER JOIN
dbo.Subsea ON dbo.Vessel.VesselId = dbo.Subsea.VesselId
First of all, does your query even work? There's no such thing as LEFT INNER JOIN ; you may have an INNER JOIN, a LEFT JOIN, or a LEFT OUTER JOIN, with the latter two being the same.
Second, I can understand your non-willingness to make an additional subquery, but why are you against the DISTINCT operator?
Third, if you use a GROUP BY and put there ONLY the fields you want, it will be equivelant to a DISTINCT operator and will return the results you need.
Last but not least, you need to show us what you are getting and what you want instead, if we are to be able to help you more.

Getting duplicates even with Select Distinct

I'm trying to write a query in which I should get the company information; however, I should be getting only 2 record and I'm getting 6 records.
Below is my query.
SELECT distinct a.FOLIO
,a.MAIN_ADDRESS1
,a.MAIN_ADDRESS2
,b.COMPANY_NAME
,b.FIRST_NAME
,b.LAST_NAME
,a.OPEN
,a.CLOSE
,c.CC
,c.CNAME
FROM vw_CODE_CASE AS a
INNER JOIN vw_CODE_CASE_VIOLATOR_CONTACTS AS b ON b.CMCODECASEID=a.CMCODECASEID
INNER JOIN vw_CODE_CASE_WORK_FLOW AS c ON c.CMCODECASEID=a.CMCODECASEID
Is it possible that is because of the amount of inner joins?
Thank you for your help.
Without sample data it would be difficult to identify that due to which column the distinct data are not coming from your query.
But you can do one thing to identify the cause, You can try remove one by one column and check the data from your query. At the point when you get your expected data, the last removed column will be the cause behind your problem.
Hope this helps.

Access 2010 - Why does a query produce less results than the base table itself

I have created a query in Access 2010 that maps a number in the right table into the left table. when i run the query i get less results than the total number of records in the left table and i wonder why? it seems that some records are left out. Does somebody have a clue on what i am doing wrong?
edit: i cant post pictures so i´ll draw this here in a simplified way:
-table 1....-........-table 2...-..........-table 3... -
-number 1-.<->.-number 1-.........-xxxxxxxx...-
-xxxxxxxx...-.......-number 2-..<->..-number 2-
Query:
SELECT [2007].[Analyse (Nummer)],
[2007].[Analyse (Name)],
[2007].Faktura, BKPF2007.Referenz,
BSET.St
FROM BSET
INNER JOIN (2007 INNER JOIN BKPF2007 ON [2007].[Faktura] = BKPF2007.[Belegnr])
ON BSET.[Belegnr] = BKPF2007.[Referenz]
GROUP BY [2007].[Analyse (Nummer)],
[2007].[Analyse (Name)],
[2007].Faktura,
BKPF2007.Art,
BKPF2007.Referenz,
BSET.St;
That's one of the functions of JOIN - it does not show records that don't match. There are a few things I can suggest without seeing specifics.
Remove one of the joins, so your query only shows Table 1 and Table 2. Make sure you get the expected results before moving on to joining Table 3
Replace your joins with LEFT JOINS which show all records even if there is no match in the right-hand table. Even if this doesn't give you the solution you're hoping for, it may help you diagnose the problem.
This is the reason for your results being filtered:
INNER JOIN
You will need to make this a LEFT JOIN to keep all results where results do not exist on the right tables(s).

Duplicate columns with inner Join

SELECT
dealing_record.*
,shares.*
,transaction_type.*
FROM
shares
INNER JOIN shares ON shares.share_ID = dealing_record.share_id
INNER JOIN transaction_type ON transaction_type.transaction_type_id = dealing_record.transaction_type_id;
The above SQL code produces the desired output but with a couple of duplicate columns. Also, with incomplete display of the column headers. When I change the
linesize 100
the headers shows but data displayed overlaps
I have checked through similar questions but I don't seem to get how to solve this.
You have duplicate columns, because, you're asking to the SQL engine for columns that they will show you the same data (with SELECT dealing_record.* and so on) , and then duplicates.
For example, the transaction_type.transaction_type_id column and the dealing_record.transaction_type_id column will have matching rows (otherwise you won't see anything with an INNER JOIN) and you will see those duplicates.
If you want to avoid this problem or, at least, to reduce the risk of having duplicates in your results, improve your query, using only the columns you really need, as #ConradFrix already said. An example would be this:
SELECT
dealing_record.Name
,shares.ID
,shares.Name
,transaction_type.Name
,transaction_type.ID
FROM
shares
INNER JOIN shares ON shares.share_ID = dealing_record.share_id
INNER JOIN transaction_type ON transaction_type.transaction_type_id = dealing_record.transaction_type_id;
Try to join shares with dealing_record, not shares again:
select dealing_record.*,
shares.*,
transaction_type.*
FROM shares inner join dealing_record on shares.share_ID = dealing_record.share_id
inner join transaction_type on transaction_type.transaction_type_id=
dealing_record.transaction_type_id;