Solved

SQL Join Query - returning duplicates - need distinct values only

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

860 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