Solved

SQL Join Query - returning duplicates - need distinct values only

Posted on 2013-01-21
5
456 Views
Last Modified: 2013-01-21
Here is my query

SELECT DISTINCT c.PartNumber, SP.SerialNumber, SP.MAC
    FROM Order C
    INNER Join SO_MAC SP
    ON C.SO = SP.SO
    Where c.SO='847403'

The problem is there are only 30 SerialNumber & MAC addresses and it's returning 60 for this current query.

The SO (Sales Order) has 2 line items - so I think it has something to do with that....if I change it to this (LI = LineItem)...Then it returns all 30 unique SerialNumber & MAC Addresses for the product.

SELECT DISTINCT c.PartNumber, SP.SerialNumber, SP.MAC
    FROM Order C
    INNER Join SO_MAC SP
    ON C.SO = SP.SO
    Where c.SO='847403' AND c.LI='1'

That's great that the above works and functions for what I need - but why if there are more than 1 "LineItems" does the JOIN create the duplicate results - and how should I be doing this query the right way so it doesn't change the results based on the number of "LineItems" on an "Order".
0
Comment
Question by:ProdigyOne2k
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:virtuadept
ID: 38802767
Yes, if SO is not unique then the join will have duplicate rows.

If SO + LI = unique and are in both tables you could do this:

SELECT DISTINCT c.PartNumber, SP.SerialNumber, SP.MAC
    FROM Order C
    INNER Join SO_MAC SP
    ON C.SO = SP.SO
    AND C.LI = SP.LI
    Where c.SO='847403'
0
 

Author Comment

by:ProdigyOne2k
ID: 38802789
The only thing "SP" and "C" tables have in common is the "SO"
0
 
LVL 8

Expert Comment

by:virtuadept
ID: 38802796
The DISTINCT should be making the rows unique.  Can you post some example output?
0
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 38802800
From what you describe it appears you do not have only one row in your Order table per order but one row per line item/PartNumber.  I thnik you are getting duplicates in your query because you are including the c.PartNumber which is probably different on each line.  Without seeing the data it is hard to know all the details and totally solve the problem.
Basically your Order table is not really normalized, i.e. distinct orders, but what most would consider the OrderDetail or OrderLineItems table.  Again, not seeing the whole picture I am having to make some assumptions here.
0
 

Author Closing Comment

by:ProdigyOne2k
ID: 38802859
This is the problem - I have normalized tables mixed with unnormalized tables causing the duplicates - no way to fix this the "right way" without doing some restructuring of the data
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now