Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Join Query - returning duplicates - need distinct values only

Posted on 2013-01-21
5
Medium Priority
?
468 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 27

Accepted Solution

by:
Chris Luttrell earned 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Viewers will learn how the fundamental information of how to create a table.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

885 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