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 > Microsoft SQL Server > To avoid duplicates in joins

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-30-09, 08:30
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:41
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Please can you provide sample output from one or both of the queries please, clearly highlighting your duplicates.
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 06-30-09, 10:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Neena John
But both of the queries return duplicate records.
no, they don't
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 06-30-09, 10:35
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Rudy is assuming that you have the appropriate constraints on both tables...
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 07-01-09, 01:17
Neena John Neena John is offline
Registered User
 
Join Date: Oct 2008
Location: India
Posts: 37
To avoid dusplicates in joins

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.

In the second table,there will be one or more records corresponding to a customer code.

Tab2

CUST CUST
CODE NAME
+------------++------------------------------++++
DD00 SUV
GG22 TRUCK PARTS INC.
AA22 THE SERVICE CENTRE
AA22 THE SERVICE CENTRE


So if we do join, it is reading all the records in the second table and showing it in resultset.
The requirement is that it should read only one
Reply With Quote
  #6 (permalink)  
Old 07-01-09, 02:25
dportas dportas is offline
Registered User
 
Join Date: Dec 2007
Location: London, UK
Posts: 732
What are the key(s) of your tables? If you just want any one name from table 2 then do:

SELECT
tab1.CUSTOMER_NO,tab1.TERMS_CODE,
tab2.CUST_NAME
FROM tab1
LEFT JOIN
(SELECT CUSTOMER_NO, MAX(CUST_NAME) CUST_NAME
FROM tab2
GROUP BY CUSTOMER_NO) tab2
ON
tab1.CUSTOMER_NO=tab2.CUSTOMER_NO ;
Reply With Quote
  #7 (permalink)  
Old 07-01-09, 02:40
Neena John Neena John is offline
Registered User
 
Join Date: Oct 2008
Location: India
Posts: 37
To avoid duplicates in joins

hi,
I tried using the query given by u.
this was the query.

SELECT
CUST_NO,COMP_NO,
CUST_NAME
FROM Tab1
LEFT JOIN
(SELECT OP_CUST_CODE, MAX(CUST_NAME) FROM
Tab2 GROUP BY OP_CUST_CODE)
Tab2
ON
CUST_NO =OP_CUST_CODE
WHERE DATE_ADD>=0 AND DATE_ADD<=9305;

But it is not accepting.
There are no primary keys in both the tables

Last edited by Neena John; 07-01-09 at 02:43.
Reply With Quote
  #8 (permalink)  
Old 07-01-09, 05:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Neena John
AA22 THE SERVICE CENTRE
AA22 THE SERVICE CENTRE
i take back what i said
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 07-01-09, 05:47
Priyafash Priyafash is offline
Registered User
 
Join Date: Jul 2009
Posts: 1
let me know if this works

i have just mentioned the tables as aa and bb, hope you would be able to relate it to your tables

select custname into #t1 from bb
where cusid in
(select aa.cusid from aa inner join (select cusid from bb group by cusid having (count(cusid) =1))bc
on aa.cusid = bc.cusid)
group by custname

create table t2 (custname varchar(10))
insert into t2(custname)
select max(custname) from bb
where cusid in
(select aa.cusid from aa inner join (select cusid from bb group by cusid having (count(cusid) >1))bd
on aa.cusid = bd.cusid)

select * from #t1 union select custname from t2



Quote:
Originally Posted by Neena John
hi,
I tried using the query given by u.
this was the query.

SELECT
CUST_NO,COMP_NO,
CUST_NAME
FROM Tab1
LEFT JOIN
(SELECT OP_CUST_CODE, MAX(CUST_NAME) FROM
Tab2 GROUP BY OP_CUST_CODE)
Tab2
ON
CUST_NO =OP_CUST_CODE
WHERE DATE_ADD>=0 AND DATE_ADD<=9305;

But it is not accepting.
There are no primary keys in both the tables
Reply With Quote
  #10 (permalink)  
Old 07-01-09, 08:08
dportas dportas is offline
Registered User
 
Join Date: Dec 2007
Location: London, UK
Posts: 732
Quote:
Originally Posted by Neena John
There are no primary keys in both the tables
Then I suggest you fix that (obviously) before you try anything else.

I can't solve the "not accepting" bit for you. What does "not accepting" actually mean?
Reply With Quote
  #11 (permalink)  
Old 07-01-09, 10:03
sqlguru sqlguru is offline
Registered User
 
Join Date: Jun 2009
Posts: 66
Why do you have duplicate data in the first place? no unique constraints?
Reply With Quote
  #12 (permalink)  
Old 07-02-09, 16:35
Megacale Megacale is offline
Registered User
 
Join Date: Apr 2009
Posts: 15
This seems extremely confusing, it might be a good idea to script out the tables and then give us some sample data to work with.
Reply With Quote
  #13 (permalink)  
Old 10-08-09, 06:40
ace8395 ace8395 is offline
Registered User
 
Join Date: Oct 2009
Posts: 1
Try this example, it worked for me and I don't get duplicate email addresses. Not sure if max is specific to MS SQL.

select e.exhibitorid,name, max(email) as email
from exhibitors e left join address a on e.exhibitorid=a.exhibitorid
group by e.exhibitorid,name
Reply With Quote
  #14 (permalink)  
Old 10-08-09, 07:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by ace8395
Not sure if max is specific to MS SQL.
roflmao

no, it is not
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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