| |
|
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.
|
 |

06-30-09, 08:31
|
|
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?
|
|

06-30-09, 09:13
|
|
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
|
|

06-30-09, 09:13
|
|
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
|
|

06-30-09, 09:45
|
|
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
|
|

06-30-09, 10:34
|
|
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
|
|

07-01-09, 01:01
|
|
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.
|
|

07-01-09, 01:48
|
|
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.
|
|

07-01-09, 01:56
|
|
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
|
|

07-01-09, 02:21
|
|
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.
|
|

07-01-09, 03:03
|
|
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.
|

07-01-09, 05:10
|
|
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
|
|

07-01-09, 06:08
|
|
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/
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|