Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Join Query - returning duplicates - need distinct values only

Posted on 2013-01-21
5
Medium Priority
?
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
Viewers will learn how the fundamental information of how to create a table.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

704 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