Solved

SQL Join Query - returning duplicates - need distinct values only

Posted on 2013-01-21
5
455 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Install MySQL 5.6 and PHP on Centos Linux 6 54
Help with SQL Query 23 39
while loop in html mail format 5 33
Syntax using Declare 3 11
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

759 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

18 Experts available now in Live!

Get 1:1 Help Now