If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > To avoid duplicates in joins

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-30-09, 08:31
Neena John Neena John is offline
Registered User
 
Join Date: Oct 2008
Location: India
Posts: 37
To avoid duplicates in joins

Hi,
There are two tables.
One table Tab1 contains cust code,cust details.
Second table Tab2 holds cust code ,cust name.
I need to get the cust name for each record in tab1 from tab 2.

I tried using the following two queries.

1)SELECT
CUSTOMER_NO,TERMS_CODE,
CUST_NAME
FROM tab1
LEFT JOIN
tab2
ON
tab1.CUSTOMER_NO=tab2.CUSTOMER_NO

2)
SELECT
a.CUSTOMER_NO,a.TERMS_CODE,b.CUST_NAME
FROM Tab1 a,
Tab2 b
where
a.CUSTOMER_NO =b.Cust_no

But both of the queries return duplicate records.
Can somebody help me to avoid the duplicates and fetch only the rows from the first table tab1?
Reply With Quote
  #2 (permalink)  
Old 06-30-09, 09:13
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Sounds like you should have a unique index on one of those tables. But you can always use DISTINCT.

Dave
Reply With Quote
  #3 (permalink)  
Old 06-30-09, 09:13
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
The following should do what you need, based on your first query:
Code:
SELECT DISTINCT
CUSTOMER_NO,TERMS_CODE, CUST_NAME 
FROM tab1 
LEFT JOIN tab2
ON tab1.CUSTOMER_NO=tab2.CUSTOMER_NO
Ax
Reply With Quote
  #4 (permalink)  
Old 06-30-09, 09:45
Cougar8000 Cougar8000 is offline
Registered User
 
Join Date: Nov 2005
Location: IL
Posts: 554
Ax,

yes it will work. But aren't you masking a bigger issue?

i.e. what Dave said plus table design as I suspect after implementing UK you will loose some records.
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows

DB2 v9.1.0.2 os 5.3.0.0
Reply With Quote
  #5 (permalink)  
Old 06-30-09, 10:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
i'm curious, neena john, are you using db2 or sql server?

--> To avoid duplicates in joins

in any case, i'll bet the results of your query do ~not~ include duplicate results
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 07-01-09, 01:01
Neena John Neena John is offline
Registered User
 
Join Date: Oct 2008
Location: India
Posts: 37
To avoid duplicates in join

I m using both DB2 and SQL

I want the number of records using the queries
SELECT COUNT(*)
FROM tab1
LEFT JOIN
tab2
ON
tab1.CUSTOMER_NO=tab2.CUSTOMER_NO

and

SELECT COUNT(*)
FROM tab1

to be same.
I tried using distinct,but it is skipping many records.
The problem is that in the second table one or more records corresponding to customer code are present.So it is showing duplicates.
Reply With Quote
  #7 (permalink)  
Old 07-01-09, 01:48
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Quote:
The problem is that in the second table one or more records corresponding to customer code are present.So it is showing duplicates.
this seems to be a design issue as mentioned in previous posts
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #8 (permalink)  
Old 07-01-09, 01:56
Neena John Neena John is offline
Registered User
 
Join Date: Oct 2008
Location: India
Posts: 37
To avoid duplicates in joins

Is there any way to read only one record (in case of duplicates)from second table using query bcoz we cant do anything regarding design
Reply With Quote
  #9 (permalink)  
Old 07-01-09, 02:21
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Create a view which shows only distinct values and use the view in the join

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #10 (permalink)  
Old 07-01-09, 03:03
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by Neena John
I want the number of records using the queries
SELECT COUNT(*)
FROM tab1
LEFT JOIN
tab2
ON
tab1.CUSTOMER_NO=tab2.CUSTOMER_NO

and

SELECT COUNT(*)
FROM tab1

to be same.
Try the following (or a variant thereof):
Code:
SELECT customer_no,terms_code, cust_name
FROM tab1
LEFT OUTER JOIN
(SELECT MIN(cust_name) AS cust_name, customer_no AS c_no
 FROM tab2 GROUP BY customer_no) X
ON customer_no=c_no
Note that you possibly loose cust_names (unless the duplicates are really "duplicate" in table tab2).
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 07-01-09 at 03:06.
Reply With Quote
  #11 (permalink)  
Old 07-01-09, 05:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Peter.Vanroose
(unless the duplicates are really "duplicate" in table tab2).
which they are, as revealed in the other thread

why the mods allow the two threads to coexist is beyoind me
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 07-01-09, 06:08
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
OK, in that case my query should do the trick.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On